Jump to content

[SOLVED] How To Use Imagecopyresized With This Water Mark Script?


phpQuestioner

Recommended Posts

I am not quit sure how to use imagecopyresized with this script or exactly how to change the width and height. Could some one give me a little guidance on this one?

 

<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource =  $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  
if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  
if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  
if (!$image) die();
$watermark = @imagecreatefromgif('08-25-2007.GIF');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);  
$watermarkwidth =  imagesx($watermark);
$watermarkheight =  imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

mainly just this:

 

$imagewidth = imagesx($image);

$imageheight = imagesy($image); 

 

But I also may need to resize this two:

 

$watermarkwidth =  imagesx($watermark);

$watermarkheight =  imagesy($watermark);

 

So that the watermark will be centered - not sure about that - still playing around with/learning GD.

I want to resize it say from 640X480 to 500X400

 

I tried this:

 

<?php
$imagewidth = imagesx($image)-140;
$imageheight = imagesy($image)-80; 
?>

 

but then it changed the location of my watermark

 

actually it did not even display watermark at all and it did not resize it either

try this addition

imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Resize after merge
$newwidth = 500;
$newheight = 400;
$resized= imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($resized, $image, 0, 0, 0, 0, $newwidth, $newheight, $imagewidth , $imageheight);
imagedestroy($image);
$image = $resized;

 

EDIT: added two lines

ok - i tried it like this:

 

<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource =  $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  
if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  
if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  
if (!$image) die();
$watermark = @imagecreatefromgif('08-25-2007.GIF');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);  
$watermarkwidth =  imagesx($watermark);
$watermarkheight =  imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Resize after merge
$newwidth = 500;
$newheight = 400;
$resized= imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($resized, $image, 0, 0, 0, 0, $newwidth, $newheight, $imagewidth , $imageheight);

//imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>

 

but nothing happened..........

Thank You MadTechie - I figured out what I was doing wrong.

 

<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource =  $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  
if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  
if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  
if (!$image) die();
$watermark = @imagecreatefromgif('08-25-2007.GIF');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);  
$watermarkwidth =  imagesx($watermark);
$watermarkheight =  imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Resize after merge
$newwidth = 500;
$newheight = 400;
$resized= imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($resized, $image, 0, 0, 0, 0, $newwidth, $newheight, $imagewidth , $imageheight);

//imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($resized);
imagedestroy($resized);
imagedestroy($watermark);

?>

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.