添加两个图像PHP

时间:2015-01-23 03:11:55

标签: php imagemagick gd image-manipulation

我需要这个: enter image description here

我这样做:

$top_file = 'image1.png';
$bottom_file = 'image2.png';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);

// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);

// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;

// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);

// save to file
imagepng($new, 'merged_image.png');

..但合并的图像没有两个图像。 PHP报告此:

Warning: imagecreatefrompng(): '/Users/myusername/Work/www/projectname/staticimage.jpg' is not a valid PNG file in /Users/myusername/Work/www/projectname/imageWatermark.php on line 60

Warning: imagecopy() expects parameter 2 to be resource, boolean given in /Users/myusername/Work/www/projectname/imageWatermark.php on line 74

1 个答案:

答案 0 :(得分:2)

正如所说/解决了评论,以及使用错误报告后显示的警告:

这两个文件都需要.png,您使用的是.jpg

  

imagecreatefrompng():'/ Users / myusername / Work / www / projectname /staticimage.jpg'

您不能混用不同的文件/图片格式,而不能将imagecreatefrompng().jpg文件一起使用。

旁注:简单地将.jpg重命名为.png将无效,因为这最终会成为损坏的文件(为初学者)并且仍然会发出警告,就像一个快速的FYI。它必须是实际的PNG图像格式。

但是,你可以这样做:(如果你想使用不同文件中的两种文件格式),则需要使用两种不同的功能及其所谓的图像格式。

$top_file = 'image1.png';
$bottom_file = 'image2.jpg';

$top = imagecreatefrompng($top_file);
$bottom = imagecreatefromjpeg($bottom_file);

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。