-2

我想在框架中上传图片并将图像和框架保存为单个图像,主要是框架中图像的大小,它应该在合并后的最终结果图像中看起来完全相同。这是我的代码的一部分:

$imgframe = $_GET['imgframe'];
$imgphoto = $_GET['imgphoto'];

$imgwidth = $_GET['imgwidth'];
$imgheight = $_GET['imgheight'];

$imgleft = substr($_GET['imgleft'],0,-2);
$imgtop = substr($_GET['imgtop'],0, -2);

$src = imagecreatefromjpeg($imgphoto);//'image.jpg'
$dest = imagecreatefrompng($imgframe);//clip_image002.png

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopyresampled( 
     $dest, $src, $imgleft, $imgtop, $imgleft, $imgtop,
     $imgwidth, $imgheight, $imgwidth, $imgheight
);
4

1 回答 1

0

根据您在问题下方的评论,您使用了错误的功能。

imagecopymerge的 PHP 手册页声明它将图像的一部分复制到另一个图像上。它声明您可以指定要从源复制的区域的原点坐标、宽度和高度,以及放置该区域的目标坐标。

换句话说,它从源图像中取出一个给定大小的矩形区域,并将其放在目标顶部的给定位置。它不询问您要复制它的区域的大小,只询问位置。因此,它不会调整复制区域的大小,而是逐个像素地复制它。

您实际需要的功能是imagecopyresampled,它允许您指定目标区域的大小,并将平滑地缩放源区域以适应。

编辑

您仍然遇到问题,因为您在源和目标参数中使用相同的坐标和相同的尺寸。相同的尺寸 == 没有调整大小。

$imgframe = $_GET['imgframe'];
$imgphoto = $_GET['imgphoto'];

// I am assuming these specify the area of the imgphoto which
// should be placed in the frame?
$imgwidth = $_GET['imgwidth'];
$imgheight = $_GET['imgheight'];
$imgleft = substr($_GET['imgleft'],0,-2);
$imgtop = substr($_GET['imgtop'],0, -2);

// now you also need to get the size of the frame so that
// you can resize the photo correctly:
$frameSize = getimagesize($imgframe);

$src = imagecreatefromjpeg($imgphoto);//'image.jpg'
$dest = imagecreatefrompng($imgframe);//clip_image002.png

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopyresampled( 
     $dest, $src, 
     0, 0, // these two specify where in the DESTINATION you want to place the source.  You might want these to be offset by the width of the frame
     $imgleft, $imgtop, // the origin of area of the source you want to copy
     $frameSize[0], $frameSize[1], // These specify the size of the area that you want to copy IN TO (i.e., the size in the destination), again you might want to reduce these to take into account the width of the frame
     $imgwidth, $imgheight // the size of the area to copy FROM
);
于 2012-07-25T16:55:28.467 回答