我想自动为用户添加水印图片帖子。
我已经完成了以下代码,但图片变为空白并带有水印。
空白图片:https://drk3g4xn3fsso.cloudfront.net/1503319365_b6ffc731d32b4abc1503319361869.jpg
<?php
function resizeimage($img,$size){
$width=$size[0];
$height=$size[1];
$newwidth = 640;
$newheight = $height*($newwidth/$width);
$tci = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $tci;
}
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function compress($source, $destination, $quality,$add_watermark=FALSE) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
$image=resizeimage($image,$info);
if ($add_watermark){
$watermark=imagecreatefrompng("watermark.png");
$infoWatermark=getimagesize("watermark.png");
$imageWidth=$info[0];
$imageHeight=$info[1];
$watermarkWidth=$infoWatermark[0];
$watermarkHeight=$infoWatermark[1];
//imagecopyresampled($image,$watermark,20,20,0,0,$watermarkWidth,$watermarkHeight,$watermarkWidth,$watermarkHeight);
imagecopy($image,$watermark,20,20,0,0,$watermarkWidth,$watermarkHeight);
}
imagejpeg($image, $destination, $quality);
return $destination;
}
我只是想知道我做错了让它与上传的图片不兼容?
谢谢。答案 0 :(得分:1)
你好这里是使用Watermark生成图像的演示。
文件夹结构:
演示
注意:watermark.jpg是您在sample.jpg图像上用作水印的图像。
这是index.php代码。
<?php
if(isset($_FILES['image_file']))
{
$max_size = 800; //max image size in Pixels
//$destination_folder = 'D:/Websites/watermark/images';
$watermark_png_file = 'watermark.png'; //watermark png file
$image_name = $_FILES['image_file']['name']; //file name
$image_size = $_FILES['image_file']['size']; //file size
$image_temp = $_FILES['image_file']['tmp_name']; //file temp
$image_type = $_FILES['image_file']['type']; //file type
switch(strtolower($image_type)){ //determine uploaded image type
//Create new image from file
case 'image/png':
$image_resource = imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_resource = imagecreatefromgif($image_temp);
break;
case 'image/jpeg': case 'image/pjpeg':
$image_resource = imagecreatefromjpeg($image_temp);
break;
default:
$image_resource = false;
}
if($image_resource){
//Copy and resize part of an image with resampling
list($img_width, $img_height) = getimagesize($image_temp);
//Construct a proportional size of new image
$image_scale = min($max_size / $img_width, $max_size / $img_height);
$new_image_width = ceil($image_scale * $img_width);
$new_image_height = ceil($image_scale * $img_height);
$new_canvas = imagecreatetruecolor($new_image_width , $new_image_height);
if(imagecopyresampled($new_canvas, $image_resource , 0, 0, 0, 0, $new_image_width, $new_image_height, $img_width, $img_height))
{
//center watermark
$watermark_left = ($new_image_width/2)-(300/2); //watermark left
$watermark_bottom = ($new_image_height/2)-(100/2); //watermark bottom
$watermark = imagecreatefrompng($watermark_png_file); //watermark image
imagecopy($new_canvas, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image
//output image direcly on the browser.
header('Content-Type: image/jpeg');
imagejpeg($new_canvas, NULL , 90);
//Or Save image to the folder
imagejpeg($new_canvas,'uploads/'.$image_name , 90);
//free up memory
//imagedestroy($new_canvas);
//imagedestroy($image_resource);
//die();
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#upload-form {
padding: 20px;
background: #F7F7F7;
border: 1px solid #CCC;
margin-left: auto;
margin-right: auto;
width: 400px;
}
#upload-form input[type=file] {
border: 1px solid #ddd;
padding: 4px;
}
#upload-form input[type=submit] {
height: 30px;
}
</style>
</head>
<body>
<form action="" id="upload-form" method="post" enctype="multipart/form-data">
<input type="file" name="image_file" />
<input type="submit" value="Send Image" />
</form>
</body>
</html>