我的主题是关于使用PHP代码为图像添加水印。示例如下http://php.net/manual/en/image.examples-watermark.php
我面临的问题是,上面提到的例子只处理JPEG图像,因为它使用imagecreatefromjpeg()函数。
我使用了这个函数,我不记得从哪里得到它。它可以创建其他类型的图像png, bmp和gif。
function imageCreateFromAny($filepath){
$type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize()
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
6 // [] bmp
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($filepath);
break;
case 2 :
$im = imageCreateFromJpeg($filepath);
break;
case 3 :
$im = imageCreateFromPng($filepath);
break;
case 6 :
$im = imageCreateFromBmp($filepath);
break;
}
return $im;
}
问题:函数的输出图像是一个图像,它的大小乘以4,我的意思是大小变大了大约4倍。例如,如果函数接收到的图像为94K,则输出大约为380K。
我想解决最大化大小的问题,我想得到相同的图像大小之前的图像大小被输入到函数imageCreateFromAny($filepath)
提示:下面的函数正在调用上面的函数
function Stamp($filename){
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('../../style/images/stamp1.png');
// $im = imagecreatefromjpeg('../../gallery/black-white/'.$filename);
$im = imageCreateFromAny('../../gallery/black-white/'.$filename);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
// header('Content-type: image/png');
// imagepng($im);
$filename_new = '../../gallery/black-white/'.$filename.'';
// if (move_uploaded_file(imagepng($im), '../../gallery/black-white/2' ))
imagepng($im, $filename_new);
imagedestroy($im);
}
您要将图像保存为PNG格式,这种格式通常比JPEG格式大得多,但质量也更高。JPEG是一种有损格式,它为较小的文件大小而放弃了质量。PNG是一种无损格式,它保留了所有可能的信息,只是尽可能地压缩数据。对于具有大量细节的图像,这将导致比低质量设置的JPEG大得多。