添加两张图片 PHP
Adding two images PHP
我需要这个:
我正在这样做:
$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
如评论中said/solved,以及您使用错误报告后显示的警告:
两个文件都必须是 .png
,而您使用的是 .jpg
。
imagecreatefrompng(): '/Users/myusername/Work/www/projectname/staticimage.jpg'
您不能混合使用两种不同的 file/image 格式,尤其是在将 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
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。
我需要这个:
我正在这样做:
$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
如评论中said/solved,以及您使用错误报告后显示的警告:
两个文件都必须是 .png
,而您使用的是 .jpg
。
imagecreatefrompng(): '/Users/myusername/Work/www/projectname/staticimage.jpg'
您不能混合使用两种不同的 file/image 格式,尤其是在将 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
旁注:错误报告只能在试运行中进行,绝不能在生产中进行。