我正在尝试编写一个小的PHP函数来拍摄图像并在图像上应用水印类型图像并将它们保存为1个图像,此代码运行时出现0错误但不应用水印图像,是有什么明显的东西会说明为什么不会这样?
<?PHP
$source_file_path ='http://cache2.mycrib.net/images/image_group92/0/43/653807d5727a46498180e8ef57fdf7819b2b0c.jpg';
$watermark_image='fpwatermark.gif'; // the watermark image
$destination_image ='coooolgif.gif'; // where to save new file
$imagesize = getimagesize($destination_image);
$watermarksize = getimagesize($watermark_image);
$watermark_x = $imagesize[0] - $watermarksize[0] - 2;
$watermark_y = $imagesize[1] - $watermarksize[1] - 2;
//run function
watermark_img($watermark_image, $destination_image, $watermark_x, $watermark_y, $watermark_w, $watermarksize[0], $watermarksize[1], $source_file_path);
function watermark_img($watermark_src, $image_src, $watermark_x, $watermark_y, $watermark_w,$watermark_h, $source_file_path) {
//Determine what type of image we're working with
list($width, $height, $type) = getimagesize($image_src);
$image_ext = $type;
switch (strtolower($image_ext)) {
#gif
case 1:
$image = imagecreatefromgif($image_src);
break;
#jpg
case 2:
$image = imagecreatefromjpeg($image_src);
imageAlphaBlending($image, true);
break;
#png
case 3:
$image = imagecreatefrompng($image_src);
imageAlphaBlending($image, true);
break;
default:
return false;
}
//Create an instance of the watermark in memory
if (!($watermark = imagecreatefromgif($watermark_src)))
return false; //Make sure your Watermark is a GIF
//Add watermark to the image
if (!(imagecopy($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
$watermark_h)))
return false;
//Resave the image with the watermark now in place
if (!(imagejpeg($image, $image_src)))
return false;
//Destroy instaces of images to free up RAM
imagedestroy($image);
imagedestroy($watermark);
//Apparently everything went well.
return $image_ext;
}
?>
答案 0 :(得分:5)
我认为你需要改变它(这是一个简单的教程http://www.cafewebmaster.com/image-watermark-php):
//Add watermark to the image
if (!(imagecopy($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
$watermark_h)))
使用:
//Add watermark to the image
if (!(imagecopymerge($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
$watermark_h, 100)))
或使用wideImage;)
答案 1 :(得分:0)
以下是如何使用透明PNG图像从水印制作的教程:http://www.sitepoint.com/article/watermark-images-php/
我之前使用过它并且有效。您拥有的代码的问题可能是您正在尝试生成jpeg,但文件扩展名是gif,因此它无法正确呈现。可能是错的。
如果它只是没有保存最终图像,那么这只是文件夹的权限问题。在这种情况下,您很可能必须通过chmod将文件夹设置为777。