【问题标题】:GD2: Resize image one, merge with image twoGD2:调整图像一的大小,与图像二合并
【发布时间】:2014-08-25 20:53:00
【问题描述】:

所以,我有一个表单,人们可以通过 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);

提前致谢!

【问题讨论】:

  • 我相信 GD 已经“贬值”了,或者被视为不受欢迎。 ImageMagick 是流行的选项,如果你有的话:php.net/manual/en/imagick.resizeimage.php
  • 听说过 ImageMagick 的好消息,但是对于这个项目我需要使用 GD2 很遗憾:)

标签: php gd2


【解决方案1】:

嗯,想通了... 不确定这是否是最佳实践(据我所知,可能使用 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

致敬!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2011-10-24
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多