2016-10-13 47 views
0

我有两个不同的图像,一个在我的本地机器,另一个在网络中。我想合并这两个图像,我拥有的一切,但我的问题是这两个图像是不一样的宽度和高度。我的本地形象是一个面具,在线图像需要适合本地。本地图像尺寸400x400。是否可以通过裁剪来固定尺寸来使用图像

问题:如果在线图像尺寸低于/大于本地图像尺寸,则图像仅取实际尺寸,不适合蒙版图像。

现在我该怎么做才能让这些图像适合对方?

My output Image

我做的尝试是[从网上收集] -

//define the width and height of our images 
define("WIDTH", 400); 
define("HEIGHT", 400); 

$dest_image = imagecreatetruecolor(WIDTH, HEIGHT); 

//make sure the transparency information is saved 
imagesavealpha($dest_image, true); 

//create a fully transparent background (127 means fully transparent) 
$trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127); 

//fill the image with a transparent background 
imagefill($dest_image, 0, 0, $trans_background); 

//take create image resources out of the 3 pngs we want to merge into destination image 
$a = imagecreatefrompng('https://rickwgrundy.files.wordpress.com/2014/01/pt509_stick_figure_podium_speaking-348x370.png'); 
$b = imagecreatefrompng('400x400.png'); 

//copy each png file on top of the destination (result) png 
imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT); 
imagecopy($dest_image, $b, 0, 0, 0, 0, WIDTH, HEIGHT); 

//send the appropriate headers and save the image in the given link name 
header('Content-Type: image/png'); 
imagepng($dest_image, "result.png", 9); 

//destroy all the image resources to free up memory 
imagedestroy($a); 
imagedestroy($b); 
imagedestroy($dest_image); 
+0

我的代码可以你给更详细的关于这其实什么ü想 –

+0

这里是我可以提供什么样的一切。我没有别的东西。 – Mysterious

+0

你想合并来自在线图片的本地图片吗? –

回答

0

而不是 imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);你需要复制和调整使用imagecopyresizedhttp://php.net/manual/en/function.imagecopyresized.php

所以你需要给图像将imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);替换为:

$image_current_width = imagesx($a); 
$image_current_height = imagesy($a); 
imagecopyresized($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT, $image_current_width , $image_current_height); 

这会调整图像的大小,并将其复制。

+0

不工作..现在图像看起来像 - [http://imgur.com/](http://imgur.com/a/q9Crr) – Mysterious

+0

嘿,不知何故,我设法可以与'imagecopyresized(... ... )'。非常感谢。 – Mysterious

0

是有可能,但您的网页图像不是400x400。你的网页图像348X370等等。如果你的本地图像348X370是100%正常工作。我尝试使用我的本地图像库在您的Web图像上工作。 my image and your web image

这里

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x,$src_y, $src_w, $src_h, $pct) 
{ 
$cut = imagecreatetruecolor($src_w, $src_h); 
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
} 

$image1 = imagecreatefrompng('https://rickwgrundy.files.wordpress.com/2014/01/pt509_stick_figure_podium_speaking-348x370.png'); //348 x 370 
$image2 = imagecreatefrompng('b.png'); //348 x 370 
imagealphablending($image1, false); 
imagesavealpha($image1, true); 

imagecopymerge_alpha($image1, $image2, 10, 9, 0, 0, 348, 370, 100); 
header('Content-Type: image/png'); 

imagepng($image1); 
+0

约22分钟前你的答案我得到了我的一个,并感谢您的时间。 – Mysterious

相关问题