如何使这个水印功能更节省内存?

时间:2011-11-04 12:49:25

标签: php memory watermark

我目前有这个水印在我的照片上打印文字。可悲的是,我不能在我的个人网站上使用它,因为它从PHP中消耗了太多内存。我现在的问题是如何使这个功能不会耗费大量内存?在我的网络托管中,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);
}

提前致谢。

1 个答案:

答案 0 :(得分:3)

用水印制作图像,然后尝试:

http://php.net/manual/en/image.examples.merged-watermark.php

编辑:此代码在我的个人服务器上运行良好。

<?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");
?>