Jump to content

Watermarking


joma

Recommended Posts

I am running oscommerce and need to be able to have staff add products to the page, problem is they are all too stupid to do things how i want them to, so i want to keep the page as dynamic as i can, i am using this to resize images if they are too large for the page, to save bandwidth and such.

 

//start
        $filename = $this->destination . $this->filename;
        $maxHeight = 498;
        $maxWidth = 664;

        $extension = substr($filename,-3);
        if($extension == 'gif')
        {
            $src = imagecreatefromgif($filename);
        }
        elseif($extension == 'png')
        {
            $src = imagecreatefrompng($filename);
        }
        elseif($extension == 'jpg'||$extension == 'jpeg')
        {
        $src = imagecreatefromjpeg($filename);

        }

        $oldWidth = imagesx($src);
        $oldHeight = imagesy($src);

        if($oldWidth > $maxWidth||$oldHeight > $maxHeight)
        {
            if(($maxWidth/$oldWidth) >= ($maxHeight/$oldHeight))
                $factor = $maxHeight/$oldHeight;
            else
                                $factor = $maxWidth/$oldWidth;
                                $newWidth = $oldWidth*$factor;
                                $newHeight = $oldHeight*$factor;
        }
        else
        {
            $newWidth = $oldWidth;
            $newHeight = $oldHeight;
        }
        $tmp = imagecreatetruecolor($newWidth,$newHeight);

        imagecopyresized($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);


         imagejpeg($tmp,$filename,80);

        imagedestroy($tmp);
        imagedestroy($src);


        //end

 

i have very basic knowledge in php and none with imagemagick. I want this script to also output a png watermark on the picture it is processing. The other thing is i have to make sure the image variables (output name and location ect..) stay intact to this script (because it is working) so that the rest of the script will continue to execute correctly.

 

cheers/  Josh

Link to comment
https://forums.phpfreaks.com/topic/81752-watermarking/
Share on other sites

Topic covered....

 

http://www.phpfreaks.com/forums/index.php/topic,170674.0.html

 

addapt varaibles

 

 

Don't forget because of the fudge factor which makes the memory used within memory limit will be alot more than ur image...

 

so make it

 

ini_set("memory_limit","40M");

 

and you won't get the server is exahusted.

 

Id recomend breaking the script up into two seperate files.

 

1 for resize image and save it

 

2 resend paths of files new and old to second file

 

3 new file watermarks image from file and outputs it

Link to comment
https://forums.phpfreaks.com/topic/81752-watermarking/#findComment-415283
Share on other sites

i am following what your saying but it is not registering to me still

 

i pass this through an upload script on the page, are you saying the script should look like this

 

//start resize
        $filename = $this->destination . $this->filename;
        $maxHeight = 498;
        $maxWidth = 664;

        $extension = substr($filename,-3);
        if($extension == 'gif')
        {
            $src = imagecreatefromgif($filename);
        }
        elseif($extension == 'png')
        {
            $src = imagecreatefrompng($filename);
        }
        elseif($extension == 'jpg'||$extension == 'jpeg')
        {
        $src = imagecreatefromjpeg($filename);

        }

        $oldWidth = imagesx($src);
        $oldHeight = imagesy($src);

        if($oldWidth > $maxWidth||$oldHeight > $maxHeight)
        {
            if(($maxWidth/$oldWidth) >= ($maxHeight/$oldHeight))
                $factor = $maxHeight/$oldHeight;
            else
                                $factor = $maxWidth/$oldWidth;
                                $newWidth = $oldWidth*$factor;
                                $newHeight = $oldHeight*$factor;
        }
        else
        {
            $newWidth = $oldWidth;
            $newHeight = $oldHeight;
        }
        $tmp = imagecreatetruecolor($newWidth,$newHeight);

        imagecopyresized($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);


         imagejpeg($tmp,$filename,80);

        imagedestroy($tmp);
        imagedestroy($src);


        //end resize

//start watermark

$watermark = ImageCreateFromGif('watermark.gif'); 
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  

//create image
if (strstr($file_allocation_new,'jpg') || strstr($file_allocation_new,'JPG') || strstr($file_allocation_new,'jpeg') || strstr($file_allocation_new,'JPEG')) {
$file_type="jpg";
}
if (strstr($file_allocation_new,'png') || strstr($file_allocation_new,'PNG')) {
$file_type="png";
}
if (strstr($file_allocation_new,'gif') || strstr($file_allocation_new,'GIF')) {
$file_type="gif";
}
switch ($file_type) {
case "jpg":
$image = imagecreatefromjpeg($file_allocation_new); 
break;
case "gif":
$image = imagecreatefromGif($file_allocation_new); 
break;
case "png":
$image = imagecreatefromPng($file_allocation_new); 
break;
}


$size = getimagesize($file_allocation_new);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5; 

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); 


switch ($file_type) {

case "jpg":

imagejpeg($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

case "gif":

imagegif($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

case "png":

imagepng($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

}

  
imagedestroy($image);  
imagedestroy($watermark);

//end watermark

 

where does the script grab the resized image though. because i think the resize one outputs the image as $filename, and i will need the watermark script to do the same. thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/81752-watermarking/#findComment-415295
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.