在添加叠加之前如何裁剪和图像? (GD) (PHP)
到目前为止我的代码(它创建了一个 YouTube 缩略图的叠加层):
<?php
header("Content-type:image/png");
$background=imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert=imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
$insert_x=imagesx($insert);
$insert_y=imagesy($insert);
imagecopymerge($background,$insert,5,57,0,0,$insert_x,$insert_y,100);
imagepng($background,NULL);
输出图像是 120x90px,但我需要它有 90x90px。
有人知道这怎么可能吗?
提前致谢!
My code so far (it creates an overlay to a youtube thumbnail):
<?php
header("Content-type:image/png");
$background=imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
$insert=imagecreatefrompng("play.png");
imagecolortransparent($insert,imagecolorat($insert,0,0));
$insert_x=imagesx($insert);
$insert_y=imagesy($insert);
imagecopymerge($background,$insert,5,57,0,0,$insert_x,$insert_y,100);
imagepng($background,NULL);
The output image is 120x90px, but i need it to have 90x90px.
Anyone knows how this is possible?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://www.johnconde。 net/blog/cropping-an-image-with-php-and-the-gd-library/
尝试它应该可以工作,如果没有评论的话。
http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
try that it should work if not comment as to otherwise.
这是取自我编写的用于创建方形缩略图的函数。您可能会发现我为自己写的评论很有帮助。根据您的需求(即,如果您无法对传入图像的类型和尺寸做出假设),您可能需要添加额外的检查。为了避免缩略图被破坏或拉伸,我们采用图像的中心部分,该部分最有可能包含可区分的内容作为源坐标。
基本思想是:由于 imagecopyresampled 或 imagecopyresized 还允许您指定目标和源坐标,您可以仅将原始图像的一部分复制到新画布并保存(或直接输出到浏览器)。为了避免尺寸被破坏或拉伸,我们选取原始图像的中心部分,该部分最有可能包含可区分的内容。
可以通过首先获取相对于其大小的中心部分,然后将其缩小到新画布来改进这一点,以处理更大的图像。
This is taken from a function I wrote to create square thumbnails. You may find the commentary I wrote for myself helpful. Depending on your needs (i.e if you can't afford to make assumptions about the type and dimensions of incoming images) you may need to add additional checks. To avoid smashed or stretched thumbnails we take a central part of the image which is most likely to contain something distinguishable as source co-ordinates.
The basic idea is: Since imagecopyresampled or imagecopyresized also allow you to specify destination and source coordinates, you can copy only part of your original image to a new canvas and save that (or directly output to browser). To avoid smashed or stretched dimensions we take a central part of the original image which is most likely to contain distinguishable content.
This could be improved to handle larger images by first taking a central part relative to it's size and then scaling it down to the new canvas.