Jump to content

Recommended Posts

I'm having great troubles watermarking my photos. The script works fine, however, my photos are suffering from color loss.

 

I'm reading about other coders using imagecreatetruecolor to prevent this from happening but I'm having a hard time getting that to work, in my situation.

 

Here is the code I was using before trying imagecreatetruecolor:

 

<?php
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);

$watermark = imagecreatefrompng('watermark.png');

$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);

if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}

$leftPixels = ($imageSize[0]/100)*$leftPercent;

$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);

imagecopyresized(
$image,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);

imagejpeg($image);

imagedestroy($image);
imagedestroy($watermark);
?>

 

... and this is the code I have tried to get working. Again the amended script works, the photo is created and sent to browser, but I still see color loss even though I'm using imagecreatetruecolor. Am I using it right?

 

<?php
$image = imagecreatefromjpeg($url);
$imageSize = getimagesize($url);

$tcImage = imagecreatetruecolor($imageSize[0], $imageSize[1]);
imagecopyresampled($tcImage, $image, 0, 0, 0, 0, $imageSize[0], $imageSize[1], $imageSize[0], $imageSize[1]);

$watermark = imagecreatefrompng('watermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);

if ($imageSize[0] > $imageSize[1] || $imageSize[0] == $imageSize[1]) {
$leftPercent = 23;
} else {
$leftPercent = 7;
}
$leftPixels = ($imageSize[0]/100)*$leftPercent;
$newWatermarkWidth = $imageSize[0]-$leftPixels;
$newWatermarkHeight = $watermark_o_height * ($newWatermarkWidth / $watermark_o_width);

imagecopyresized(
$tcImage,
$watermark,
$imageSize[0]/2 - $newWatermarkWidth/2,
$imageSize[1]/2 - $newWatermarkHeight/2,
0,
0,
$newWatermarkWidth,
$newWatermarkHeight,
imagesx($watermark),
imagesy($watermark)
);

imagejpeg($tcImage,NULL,100);

imagedestroy($image);
imagedestroy($tcImage);
imagedestroy($watermark);
?>

 

And I have attached a screenshot of the color difference against my thumbnails, which are perfect.

 

My thumbnails use readfile() and output perfectly. If I change my watermarking script to use readfile() they also load perfectly. It's just an issue with imagecreatefromjpeg and imagecopyresized.

 

Please, somebody, help put me straight on this.

post-135509-13482403639853_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/
Share on other sites

I doubt the watermark is the issue?  May just be the result of jpegging a jpeg?

 

Try it without the watermark and see what happens?

 

Also, are you sure your watermark doesn't have like a 5% white bg overlay?  That could be washing out your photo.


   // Resizing images with GD without color loss
    $src = imagecreatefromstring(file_get_contents($source));
    ImageCopyResized($dst, $src, 0, 0, 0, 0, $t_width, $t_height, ImageSX($src), ImageSY($src));
    Imagejpeg($dst, $dest, 90);
           
    $dst = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($dst, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

 

Darks example is a bit jumbled, couldn't work it out. I also tried 3 different variations of the script, without any luck. I think I have come to the conclusion that GD cannot deal with image manipulation just yet. Somebody has reported the bug on PHP website. I'm now seeing a lot of articles and complaints that some color profiles are missing when using GD 1 or 2. Everyone says to use Imagick instead for color perfect photos. I checked my photos and they are set to sRGB.

here is something i read on stack overflow.com

 

 

 

As you said yourself, JPEG is a lossy format. It doesn't actually store "pixels" directly. If you make a change to the image, the image has to be re-compressed. There is no way around this.

 

The reason your red pixel is the "wrong color" and "blurred" is because of how JPEG compression works. Again, it doesn't store pixels. It puts emphasis on changes in brightness, and actual color information doesn't matter so much.

 

I'm not positive, but you may be able to only re-compress the few blocks that are affected by your change. You would not be able to do this with any standard functions, and would have to dig into the format and compression schemes yourself.

 

In Other words there is NO solution in GD image functions for PHP however you could try this image magick example i don't know if it would work any better than GD.

 

<?php
$images = new Imagick(glob('images/*.jpg'));

foreach($images as $image)
{
    // compression methods, see "Contants"-page for Imagick
    $image->setCompression(imagick::COMPRESSION_JPEG);
    // a value between 1 and 100, 1 = high compression, 100 low compression
    $image->setCompressionQuality(80);
    $image->writeImage();
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.