Jump to content

GD help. Anyone shed some Light!


smonkcaptain

Recommended Posts

Hello all.

 

I've recently made a watermark script to apply a watermark and copyright footer to any uploaded photograph on my site.

The script is shown below:

<?php
ini_set("memory_limit","512M");

$username="Jimmy Leaman";

if(isset($_GET['id'])){
$id = $_GET['id'];	
}
if(isset($_GET['option'])){
$option = $_GET['option'];
}
if(isset($_GET['strength'])){
$strength= $_GET['strength'];
}
if(isset($_GET['size'])){
$size = $_GET['size'];
}

if(isset($_GET['position'])){
$position = $_GET['position'];
}

if(isset($_GET['width'])){
$width = $_GET['width'];
}



if($option=="no"){
$stamp = imagecreatefrompng('none.png');
}

if($strength=="verylight" & $size=="small"){
$stamp = imagecreatefrompng('watermarks_vlight.png'); 
}

if($strength=="verylight" & $size=="med"){
$stamp = imagecreatefrompng('watermarkm_vlight.png');
}

if($width>="1024" & $strength=="verylight" & $size=="large"){
$stamp = imagecreatefrompng('watermarkl_vlight.png');
}

if($width<="1024" & $strength=="verylight" & $size=="large"){
$stamp = imagecreatefrompng('watermarks_vlight.png');
}

if($strength=="verylight" & $size=="verylarge"){
$stamp = imagecreatefrompng('watermarkxl_vlight.png');
}




if($strength=="light" & $size=="small"){
$stamp = imagecreatefrompng('watermarks_light.png'); 
}

if($strength=="light" & $size=="med"){
$stamp = imagecreatefrompng('watermarkm_light.png');
}

if($width>="1024" & $strength=="light" & $size=="large"){
$stamp = imagecreatefrompng('watermarkl_light.png');
}

if($width<="1024" & $strength=="light" & $size=="large"){
$stamp = imagecreatefrompng('watermarks_light.png');
}

if($strength=="light" & $size=="verylarge"){
$stamp = imagecreatefrompng('watermarkxl_light.png');
}




if($strength=="med" & $size=="small"){
$stamp = imagecreatefrompng('watermarks_med.png');
}

if($strength=="med" & $size=="med"){
$stamp = imagecreatefrompng('watermarkm_med.png');
}

if($width>="1024" & $strength=="med" & $size=="large"){
$stamp = imagecreatefrompng('watermarkl_med.png');
}

if($width<="1024" & $strength=="med" & $size=="large"){
$stamp = imagecreatefrompng('watermarks_med.png');
}

if($strength=="med" & $size=="verylarge"){
$stamp = imagecreatefrompng('watermarkxl_med.png');
}





if($strength=="strong" & $size=="small"){
$stamp = imagecreatefrompng('watermarks_strong.png');
}
if($strength=="strong" & $size=="med"){
$stamp = imagecreatefrompng('watermarkm_strong.png');
}
if($width>="1024" & $strength=="strong" & $size=="large"){
$stamp = imagecreatefrompng('watermarkl_strong.png');
}

if($width<="1024" & $strength=="strong" & $size=="large"){
$stamp = imagecreatefrompng('watermarks_strong.png');
}

if($strength=="strong" & $size=="verylarge"){
$stamp = imagecreatefrompng('watermarkxl_strong.png');
}

list($width, $height) = getimagesize('../queued/'.$id.'.jpg');
$sx = imagesx($stamp);
$sy = imagesy($stamp);


if($position=="top"){
$marge_right = ($width-$sx)/2;
    $marge_bottom = ($height)-(0.07*$height)-($sy)+20;
}

if($position=="centre"){
$marge_right = ($width-$sx)/2;
    $marge_bottom = ($height-$sy)/2;
}

if($position=="bottom"){
$marge_right = ($width-$sx)/2 ;
    $marge_bottom = (0.07*$height);
}





// Load the stamp and the photo to apply the watermark to
$im = imagecreatefromjpeg('../queued/'.$id.'.jpg');

// Set the margins for the stamp and get the height/width of the stamp image

$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));



// Create the image
$cim = imagecreatetruecolor($width/2,20);

// Create some colors
$white = imagecolorallocate($cim, 255, 255, 255);
$grey = imagecolorallocate($cim, 128, 128, 128);
$black = imagecolorallocate($cim, 0, 0, 0);
imagefilledrectangle($cim, 0, 0, 399, 29, $black);


// The text to draw
$text = 'Aviation-Photographs.net';
// Replace path by your own font path
$font = 'BOMBARD.ttf';

// Add the text
imagettftext($cim, 13, 0, $width/2-195, 15, $white, $font, $text);

imagecopy($im, $cim, $width-$width/2, $height-20, 0, 0, imagesx($cim), imagesy($cim));


// Create the image
$cim2 = imagecreatetruecolor($width/2,20);

// Create some colors
$white2 = imagecolorallocate($cim2, 255, 255, 255);
$grey2 = imagecolorallocate($cim2, 128, 128, 128);
$black2 = imagecolorallocate($cim2, 0, 0, 0);
imagefilledrectangle($cim2, 0, 0, 399, 29, $black);


// The text to draw
$text2 = 'Copyright - '.$username.'';
// Replace path by your own font path
$font2 = 'EuroScope.ttf';

// Add the text
imagettftext($cim2, 10, 0, 10, 14, $white2, $font2, $text2);

imagecopy($im, $cim2, 0, $height-20, 0, 0, imagesx($cim2), imagesy($cim2));

imagejpeg($im, '../queued/'.$id.'_b.jpg', 100);

// Using imagepng() results in clearer text compared with imagejpeg()
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);





?>

 

So the script gets the image that is already from the upload form, and then applies the watermark and then overwrites and saves the .jpg image.

 

My problem is that, the final watermarked image seems to have changed colour slightly,  become slightly softer and also decreased in file size! I'm totally puzzled as to why this is, as none of the code it above (i think) has actually changed any of the colours of compressed the image.

 

The image straight from the upload form seems to be top notch, however after appling the watermark and resaving, something is lost, and i don't know what.

 

Can someone please read my code and shed some light on the issue, just so i can work towards maintaing the image quality after the watermark is applied.

 

Thanks all in advanced! Jimmy Leaman.. :)

Link to comment
https://forums.phpfreaks.com/topic/208361-gd-help-anyone-shed-some-light/
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.