2

I am creating a thumbnail of myImage of various format (jpg, png). Here is my code

<?php
error_reporting(E_ALL);
//$file = 'H:/xampp/htdocs/myfolder/new.jpg';
$file = 'H:/xampp/htdocs/myfolder/phool.png';
$pathToSave = 'H:/xampp/myfolder/New/';
$sourceWidth =60;
$sourceHeight = 60;
$what = getimagesize($file);
$file_name = basename($file);
//print "MIME ".$what['mime'];
switch(strtolower($what['mime']))
{
    case 'image/png':
            $img = imagecreatefrompng($file);
            $img = imagecreatefromstring($img);
            $new = imagecreatetruecolor($what[0],$what[1]);
            imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
            header('Content-Type: image/png');
            imagepng($new,$pathToSave.$file_name);
            imagedestroy($new);
        break;
    case 'image/jpeg':
            $img = imagecreatefromjpeg($file);
            $new = imagecreatetruecolor($what[0],$what[1]);
            imagecopy($new,$img,0,0,0,0,$what[0],$what[1]);
            header('Content-Type: image/jpeg');   
            imagejpeg($new,$pathToSave.$file_name);
            imagedestroy($new);
        break;
    case 'image/gif':
        $img = imagecreatefromgif($file);
        break;
    default: die();
}

But, It is not working for PNG images.It saves a blank image in my destination $pathToSave = 'H:/xampp/htdocs/myfolder/New/'.

If I remove $img = imagecreatefromstring($img); then imagepng saves the image as it is but does not compress the image as imagejpg does.

Hitesh
  • 4,098
  • 11
  • 44
  • 82
  • just try without `$img = imagecreatefromstring($img);` – Jothi Kannan Oct 09 '14 at 14:00
  • good observation but if I remove that it just saves original image, It does not compress it – Hitesh Oct 09 '14 at 14:05
  • here what you mean original image ? your file `phool.png` so it may same image – Jothi Kannan Oct 09 '14 at 14:12
  • here is the image I am using http://upload.wikimedia.org/wikipedia/commons/e/e6/Bosje_bloemen.png after saving in my local – Hitesh Oct 09 '14 at 18:02
  • lets say my png file is of 4 mb , then after recreating using `imagecreatefrompng` it should create smaller image but it is creating image of same size – Hitesh Oct 09 '14 at 18:13
  • what makes you think using `imagecreatefrompng()` should make it smaller? this only reads an image. the size of the resulting file is determined by the compression parameter in `imagepng()`. If you want a smaller file you have to use `imagepng($new, $pathToSave.$file_name, 9);` – Gerald Schneider Oct 10 '14 at 06:15
  • @GeraldSchneider : thanks !!! but same code `image/jpeg` compression works whereas it is not compressing when I use `imagepng` , But thanks I will try your suggesstion – Hitesh Oct 10 '14 at 07:29
  • @GeraldSchneider : could you give some info about this last parameter in `imagepng`. I checked the manual but it is not making sense ...http://php.net/manual/en/function.imagepng.php – Hitesh Oct 10 '14 at 07:37
  • got it `imagepng` parameter `quality` `0-9` but it is only reducing to some mb even when I keep it 9. for example- 3.7 mb file is reduced to 3 mb thats all ... but for `imagejpg` same size file reduced around 1mb – Hitesh Oct 10 '14 at 08:45
  • Sometimes you just can't reduce images further. JPEG doesn't compress, it reduces image quality. The more you reduce the quality, the smaller the file gets. PNG uses real compression without quality loss. You have to choose the file format according to the type of the image. For example, PNG is better suited for screenshots (large areas with the same color) then JPEG. JPEG on the other hand is better suited for actual photos (the quality loss isn't as visible there). – Gerald Schneider Oct 10 '14 at 09:00
  • @GeraldSchneider-- hmmm Could you add your answer explaining both little bit and why it is not compressed more in png I will accept your answer ...because your suggestion seems to work....also if you like question pls upvote – Hitesh Oct 10 '14 at 09:30
  • Well, I could, but it wouldn't answer the question "why does it create a blank image". This problem has already been answered. Why the saved image doesn't get smaller is a completely different question. – Gerald Schneider Oct 10 '14 at 09:35

2 Answers2

1
$img = imagecreatefrompng($file); //Creates an image from a png file
$img = imagecreatefromstring($img); //Creates an image from a string

Basicly you are creating an image in memory using imagecreatefrompng($file) but then you overwrite the same variable using imagecreatefromstring($img) which doesn't take a GDImage variable as parameter so it returns a blank image (or null?).

Just remove imagecreatefromstring($img); and it will work.

$newImage = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrompng($fileurl);
imagecopyresized($newImage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage,$fileurl);
imagedestroy($newImage);
imagedestroy($source);
Quovadisqc
  • 81
  • 7
  • Can you please try downloading some higher resolution image of png and copy my code in your local ... and try to do what you are explaining me here. I am trying this but it is saving the same original image and does not make it smaller image .... where as it works for `image/jpeg` – Hitesh Oct 09 '14 at 17:57
  • try this image http://upload.wikimedia.org/wikipedia/commons/e/e6/Bosje_bloemen.png – Hitesh Oct 09 '14 at 17:59
  • Removing this `imagecreatefromstring($img)` , works for removing blank image – Hitesh Oct 10 '14 at 09:53
0

try this, it is working to me

$img  = imagecreatefrompng( $file);
$new  = imagecreatetruecolor( $what[0],$what[1]);
imagecopyresampled( $new, $img, 0,0,0,0,$what[0],$what[1] );

reference for imagecopyresampled

Jothi Kannan
  • 3,320
  • 6
  • 40
  • 77