在添加叠加之前如何裁剪和图像? (GD) (PHP)

发布于 2024-11-05 23:26:00 字数 524 浏览 1 评论 0原文

到目前为止我的代码(它创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

机场等船 2024-11-12 23:26:00

http://www.johnconde。 net/blog/cropping-an-image-with-php-and-the-gd-library/

<?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));

list($current_width, $current_height) = getimagesize($background);

$left = 0;
$top = 0;

$crop_width = 90;
$crop_height = 90;

$canvas = imagecreatetruecolor($crop_width, $crop_height);

$current_image = $background;

imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

imagecopymerge($canvas,$insert,5,57,0,0,$current_width,$current_height,100);

imagepng($canvas);

?>

尝试它应该可以工作,如果没有评论的话。

http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/

<?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));

list($current_width, $current_height) = getimagesize($background);

$left = 0;
$top = 0;

$crop_width = 90;
$crop_height = 90;

$canvas = imagecreatetruecolor($crop_width, $crop_height);

$current_image = $background;

imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);

imagecopymerge($canvas,$insert,5,57,0,0,$current_width,$current_height,100);

imagepng($canvas);

?>

try that it should work if not comment as to otherwise.

一抹苦笑 2024-11-12 23:26:00

这是取自我编写的用于创建方形缩略图的函数。您可能会发现我为自己写的评论很有帮助。根据您的需求(即,如果您无法对传入图像的类型和尺寸做出假设),您可能需要添加额外的检查。为了避免缩略图被破坏或拉伸,我们采用图像的中心部分,该部分最有可能包含可区分的内容作为源坐标。

基本思想是:由于 imagecopyresampledimagecopyresized 还允许您指定目标和源坐标,您可以仅将原始图像的一部分复制到新画布并保存(或直接输出到浏览器)。为了避免尺寸被破坏或拉伸,我们选取​​原始图像的中心部分,该部分最有可能包含可区分的内容。

//assuming you have already merged your $background image with your overlay at this point

//original dimensions
$original_width = imagesx($background);
$original_height = imagesy($background);

//new dimensions
$new_width = 90;
$new_height = 90;

//the center of the rectangular image
$center = $original_width/2, $original_height/2

//the coordinates from where to start on the original image 
//assuming you want to crop the center part
$src_x = floor(($original_width/2) - ($new_width/2)); 
$src_y = floor(($original_height/2) - ($new_height/2)); 

//create a new canvas with the new desired width, height
$temp = imagecreatetruecolor(90,90);

//copy the large image to this new canvas
imagecopyresampled($temp,$background,0,0,$src_x,$src_y,$new_width,$new_height,$original_width,$original_height);
//from right to left: source image, destination image, dest x, dest y,
//source x, source y, new width, new height, original width, original height

//save the canvas as an image
imagepng($temp);

可以通过首先获取相对于其大小的中心部分,然后将其缩小到新画布来改进这一点,以处理更大的图像。

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.

//assuming you have already merged your $background image with your overlay at this point

//original dimensions
$original_width = imagesx($background);
$original_height = imagesy($background);

//new dimensions
$new_width = 90;
$new_height = 90;

//the center of the rectangular image
$center = $original_width/2, $original_height/2

//the coordinates from where to start on the original image 
//assuming you want to crop the center part
$src_x = floor(($original_width/2) - ($new_width/2)); 
$src_y = floor(($original_height/2) - ($new_height/2)); 

//create a new canvas with the new desired width, height
$temp = imagecreatetruecolor(90,90);

//copy the large image to this new canvas
imagecopyresampled($temp,$background,0,0,$src_x,$src_y,$new_width,$new_height,$original_width,$original_height);
//from right to left: source image, destination image, dest x, dest y,
//source x, source y, new width, new height, original width, original height

//save the canvas as an image
imagepng($temp);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文