PaparazzoKid Posted July 29, 2012 Share Posted July 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/ Share on other sites More sharing options...
smoseley Posted July 29, 2012 Share Posted July 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365225 Share on other sites More sharing options...
darkfreaks Posted July 29, 2012 Share Posted July 29, 2012 // 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); Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365229 Share on other sites More sharing options...
smoseley Posted July 29, 2012 Share Posted July 29, 2012 dark, that code seems a little jumbled? Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365237 Share on other sites More sharing options...
PaparazzoKid Posted July 29, 2012 Author Share Posted July 29, 2012 I'll give this a try. Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365263 Share on other sites More sharing options...
PaparazzoKid Posted July 29, 2012 Author Share Posted July 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365298 Share on other sites More sharing options...
darkfreaks Posted July 29, 2012 Share Posted July 29, 2012 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266405-color-loss-in-photos-after-watermarking/#findComment-1365299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.