所以,我有一个人们可以通过AJAX上传图像的表格。 图像上传后,他们可以从包含另一个图像(.png)的列表中进行选择,这些图像可以拖放到第一张图像上。
一旦他们对结果感到满意,按下按钮就可以合并两个图像。即使有位置,这也很好用 - 直到我想给大家重新调整第二张图像的大小(他们可以拖放上传的图像)。
我现在的代码无法调整小图片的大小。实际上,它甚至没有显示在上传的图像(背景图像)上。我在这里缺少什么?
//receiving some values from an AJAX call
//those values referes to the background image
$pathToImage = $_POST['pathToImage'];
$posBg = $_POST['posBg'];
$fileUploaded = '../'.$pathToImage;
//those values referes to the image that goes on top of the background
$posTop = $_POST['posTop'];
$posLeft = $_POST['posLeft'];
$itemWidth = $_POST['itemWidth'];
$fileChupon = '../images/chupon1.png';
//my target file
$targetfile = "../images/galeria/testing".time().".png";
//here's the issue.. I am trying to resize the small image that goes on top of the background - this doesn't work and with this piece of code nothing is shown on top of the background
$chuponCreated = imagecreatefrompng($fileChupon);
$newWidth = $itemWidth;
$newHeight = $itemWidth;
$tmp = imagecreatetruecolor($newWidth,$newHeight);
$chupon = imagecopyresampled($tmp, $chuponCreated,0,0,0,0,$newWidth,$newHeight,250,250);
//background image is shown in the dimensions that it's supposed to
$fondo = imagecreatefromjpeg($fileUploaded);
$fondoW = imagesx($fondo);
$fondoH = imagesy($fondo);
$photoFrame = imagecreatetruecolor($fondoW,303);
imagecopyresampled($photoFrame,$fondo,0,$posBg,0,0,$fondoW,$fondoH,$fondoW,$fondoH);
//here trying to add the small image over the background
imagecopy($photoFrame,$chupon,$posLeft,$posTop,0,0,$itemWidth,$itemWidth);
imagejpeg($photoFrame, $targetfile);
$imgPath = $targetfile;
$image = imagecreatefromjpeg($imgPath);
imagejpeg($image);
提前致谢!
答案 0 :(得分:0)
好吧,想通了...... 不确定这是否是最佳实践(并且可能使用ImageMagick,结果将从我所听到的内容中获得更好的质量)。
无论如何,我会发布它,以防其他人需要这样的解决方案。
$pathToImage = $_POST['pathToImage'];
$posBg = $_POST['posBg'];
$posTop = $_POST['posTop'];
$posLeft = $_POST['posLeft'];
$itemWidth = $_POST['itemWidth'];
$fileChupon = '../images/chupon1.png';
$fileUploaded = '../'.$pathToImage;
$targetfile = "../images/galeria/testing".time().".png";
//to resize the second image start
$percent = 0.5;
list($width, $height) = getimagesize($fileChupon);
$newwidth = $width * $percent;
$newheight = $height * $percent;
$chupon = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($chupon, false);
imagesavealpha($chupon,true);
$transparent = imagecolorallocatealpha($chupon, 255, 255, 255, 127);
imagefilledrectangle($chupon, 0, 0, $newwidth, $newheight, $transparent);
$source = imagecreatefrompng($fileChupon);
imagecopyresized($chupon, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//to resize the second image end
$fondo = imagecreatefromjpeg($fileUploaded);
$fondoW = imagesx($fondo);
$fondoH = imagesy($fondo);
$photoFrame = imagecreatetruecolor($fondoW,303);
imagecopyresampled($photoFrame,$fondo,0,$posBg,0,0,$fondoW,$fondoH,$fondoW,$fondoH);
imagecopy($photoFrame,$chupon,$posLeft,$posTop,0,0,$newwidth,$newheight);
imagejpeg($photoFrame, $targetfile);
$imgPath = $targetfile;
$image = imagecreatefromjpeg($imgPath);
imagejpeg($image);
我的更改来源是:
PHP.net调整图片大小:http://php.net/manual/en/function.imagecopyresized.php
Stackoverflow使.png背景透明:https://stackoverflow.com/a/279310/3293843
Saludos!