Page 1 of 1

Problem with imagecreatefrompng

Posted: Wed Nov 24, 2010 9:30 am
by YeaTii
Hello folks,

I just discovered this forum and I hope you will be able to help me :D

I have this weird problem with this code :

Code: Select all

<?php
// ouvre l'image d'origine
$theimage = "upload/images/tmp/logo.png";
$im = imagecreatefrompng($theimage);

// détermine sa taille
$size = getimagesize($theimage);
$w = $size[0];
$h = $size[1];

// crée l'image de sortie
$im2 =  imagecreatetruecolor($w/2,$h/2);
imagealphablending($im2,false);
imagesavealpha($im2,true);

// remplit l'image de sortie
imagecopyresampled($im2,$im,0,0,0,0,$w/2,$h/2,$w,$h);

// affiche l'image
header("Content-type: image/x-png" );
imagepng($im2);
?>
As you can see, it's a simple crop code to work with jcrop.

Now here is the tricky part : I'm using KCfinder for image uploading/management and I created a page where you can "jcrop" any image from the library.
Please, keep in mind that, to avoid any problem, I set all folders and file to 777 !!
Everything works fine with jpeg (imagecreatefromjpg) image part (same code except for the function calls) !

My problem comes with png images uploaded with KCfinder, somehow, they don"t have the same owner/group or something but I cannot understand how this could change anything with a 777 permission.

If I use imagecreatefromJPG for my png, it works but I lose the png's transparency.
If I use the code above with a ftp uploaded picture, it works !
If I use the code above with a KCFinder uploaded picture, I get this error :

Code: Select all

Warning: imagecreatefrompng() [function.imagecreatefrompng]: 'upload/images/tmp/logo.png' is not a valid PNG file in /test.php on line 4
Now, how can we explain that I have this error with a 777 mod and the exast same file/path would work with imagecreatefromJPG ??
:crazy:

Re: Problem with imagecreatefrompng

Posted: Wed Nov 24, 2010 10:34 am
by Weirdan
Because it's jpeg, not png.

As far as I tell from the source code, the uploader you use resizes the file on upload, saving it as jpeg. So it's jpeg internally, even if it's named *.png

Re: Problem with imagecreatefrompng

Posted: Wed Nov 24, 2010 10:44 am
by YeaTii
OK, you were right, KCFinder change png to jpg because I resized upload !!! This is KCFinder crap.

thanks !

Re: Problem with imagecreatefrompng

Posted: Wed Nov 24, 2010 10:53 am
by Weirdan
No, I think I've got everything right.
I'm using KCfinder for image uploading/management
It saves all images in jpeg - see uploader::imageResize() method called from uploader::checkUploadedFile() called from uploader::upload()
uploader::imageResize() specifically calls $gd->imagejpeg(), regardless of the image type you've uploaded.

That's also the reason why it works with imagecreatefromjpeg() - because it is jpeg

Edit:
Please do

Code: Select all

var_dump($size);
after the getimagesize()

Code: Select all

// détermine sa taille
$size = getimagesize($theimage);
and post the results here.

Re: Problem with imagecreatefrompng

Posted: Wed Nov 24, 2010 11:04 am
by YeaTii
Thanks :)

I checked it and yes, it is jpeg, now I understand :D

I remove the resizing from KCFinder, now it works perfect !!

this is just great, I will do the resize some other ways then...

You are the BEST for helping me and answering so fast !!!!