Jump to content

PHP GD colour problems


smonkcaptain

Recommended Posts

Hey all!

 

I've just finished my watermarking system on my website.

 

Once the watermark is uploaded, it displays it as a PNG file, as seen in this link:

 

http://www.aviation-photographs.net/members/watermark.php?id=1279489924&size=verylarge&strength=strong&position=bottom&width=1500

 

In my PHP, it then saves the file as a JPG, which then appears in the user's 'photo' section, as seen here:

 

http://www.aviation-photographs.net/members/view_queued.php?id=1279489924

 

From what i can quite clearly see, is that there is a colour difference between the two photographs and also a quality loss, seen most on the 'Air Canada' titles...

 

I was just wondering why this is happening, as the quality of the photograph is the most important aspect on my site!

 

Here is my code:

 

Upload.php:

 

<?php
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '../queued/';

// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . '../members/photoupload.php'; 

// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . '../members/upload_success.php';

// fieldname used within the file <input> of the HTML form
$fieldname = 'photo'; 

// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
                2 => 'html form max file size exceeded',
                3 => 'file upload was only partial',
                4 => 'no file was attached');

// check the upload form was actually submitted else print the form
isset($_POST['submit'])
    or error('the upload form is neaded', $uploadForm);

// check for PHP's built-in uploading errors
($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
    
// check that the file we are working on really was the subject of an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $uploadForm);
    
// validation... since this is an image upload script we should run a check  
// to make sure the uploaded file is in fact an image. Here is a simple check:
// getimagesize() returns false if the file tested is not an image.
@getimagesize($_FILES[$fieldname]['tmp_name'])
    or error('only image uploads are allowed', $uploadForm);
    
// make a unique filename for the uploaded file and check it is not already
// taken... if it is already taken keep trying until we find a vacant one
// sample filename: 1140732936-filename.jpg
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'_thumb.jpg'))
{
    $now++;
}

// now let's move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $uploadForm); 

//This gets all the other information from the form 
$genre=$_POST['genre'];
$aircraft=$_POST['aircraft']; 
$airline=$_POST['airline']; 
$airport=$_POST['airport']; 
$country=$_POST['country'];
$registration=$_POST['registration'];
$connumber=$_POST['connumber'];
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$remarks=$_POST['remarks']; 
$photo=($_FILES['photo']);
$icao=$_POST['icao'];
$iata=$_POST['iata'];

$getid=mysql_query("SELECT id FROM `members` WHERE username LIKE '$username'");
while($id=mysql_fetch_array($getid)){

$photographerid= $id['id'];
}



$date=$day.'-'.$month.'-'.$year;

foreach($_POST['tag'] as $tag){
   if($tag){
       $tags[] = $tag;
   }
}
$tag = implode('+',$tags);

//Writes the information to the database 
mysql_query("INSERT INTO `queued_photos` (`username`, `photographerid`, `genre`, `aircraft`, `airline`, `country`, `airport`, `registration`,`msn`, `category`, `date`, `photoname`, `remarks`) VALUES ('$username', '$photographerid', '$genre', '$aircraft', '$airline', '$country', '$airport', '$registration', '$connumber', '$tag', '$date', '$now', '$remarks')")or die(mysql_error());

$img=imagecreatefromjpeg('../queued/'.$now.'_thumb.jpg');
$imwidth= imagesx($img);

$position=$_POST['position'];
$size=$_POST['size'];
$strength=$_POST['strength'];
$watermark =$_POST['watermark'];
if($watermark=="No"){
 header("Location: watermark.php?id=$now&option=no"); 
}elseif ($watermark=="Yes"){


// If you got this far, everything has worked and the file has been successfully saved.
// We are now going to redirect the client to a success page.
header("Location: watermark.php?id=$now&size=$size&strength=$strength&position=$position&width=$imwidth");

}
?>

 

Watermark.php:

<?php
session_start();
$username=$_SESSION['username'];

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.'_thumb.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.'_thumb.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));

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


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


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

// 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.'.jpg', 100);

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





?>

 

 

 

Thankyou all in advanced.

 

Link to comment
https://forums.phpfreaks.com/topic/208119-php-gd-colour-problems/
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.