[英]How can I make this watermark function more memory-efficient?
I currently have this watermark to print a text on my photos. 我目前有这个水印,可以在照片上打印文字。 The sad thing is that I can't use it on my personal website, because it drains too much memory from PHP. 可悲的是,我无法在我的个人网站上使用它,因为它消耗了PHP太多的内存。 My question is now how I can make this function to not drain to much memory? 现在我的问题是如何使该功能不消耗太多内存? The memory_limit is set to 64M at my web hosting. 在我的虚拟主机上,memory_limit设置为64M。
function watermark($source, $text, $destination) {
list($width, $height) = getimagesize($source);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($source);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font = 'fonts/acknowtt.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 16, 21, $black, $font, $text);
imagettftext($image_p, $font_size, 0, 15, 20, $white, $font, $text);
if($destination <> '') {
imagejpeg($image_p, $destination, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
}
imagedestroy($image);
imagedestroy($image_p);
}
Thanks in advance. 提前致谢。
Make an image out of your watermark then try this: 用水印制作图像,然后尝试以下操作:
http://php.net/manual/en/image.examples.merged-watermark.php http://php.net/manual/zh/image.examples.merged-watermark.php
EDIT: This code works well on my personal server. 编辑:此代码在我的个人服务器上运行良好。
<?php
//This function creates a watermark image from the text you supply
//and creates a file called watermark.jpg
//You might need to enable write access for the owner (chmod 644)
function watermark($text) {
//adjust the size of your watermark, here it's 300x100
$image_p = imagecreatetruecolor(300, 100);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
//change this to your font file
$font = 'arial.ttf';
$font_size = 10;
imagettftext($image_p, $font_size, 0, 16, 21, $black, $font, $text);
imagettftext($image_p, $font_size, 0, 15, 20, $white, $font, $text);
imagejpeg($image_p, 'watermark.jpg', 100);
//this will save some memory
imagecolordeallocate($image_p, $black);
imagecolordeallocate($image_p, $white);
imagedestroy($image_p);
}
//this function merges the watermark with the image of your choosing
function merge_image($source,$destination){
//set the memory limit to handle your image, my image size is 639KB on disc
//but this value is calculated like this (from http://us2.php.net/manual/en/function.imagecreatefromjpeg.php):
//The memory required to load an image using imagecreatefromjpeg() is a function
//of the image's dimensions and the images's bit depth, multipled by an overhead.
//It can calculated from this formula:
//Num bytes = Width * Height * Bytes per pixel * Overhead fudge factor
//Where Bytes per pixel = Bit depth/8, or Bits per channel * Num channels / 8.
ini_set('memory_limit', '100M');
list($w,$h)=getimagesize($source);
$image=imagecreatefromjpeg($source);
$watermark=imagecreatefromjpeg('watermark.jpg');
list($ww,$wh)=getimagesize('watermark.jpg');
//HERE you need to adjust some values to get the watermark where you want it to be
//x and y position of watermark in new image
$x=280;
$y=5;
//opacity of watermark
$o=100;
//get the same black color from the watermark
$black = imagecolorallocate($watermark,0,0,0);
//set that color as transparent
imagecolortransparent($watermark,$black);
imagecopymerge($image, $watermark, $x, $y, 0, 0, $ww, $wh, $o);
imagejpeg($image, $destination, 100);
}
//create the watermark
watermark("09-26-1998");
//merge with 1.jpg (in the same folder) and name it newimage.jpg
merge_image("./1.jpg","newimage.jpg");
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.