Using the PHP GD library to resize and save images is HELL

I am writing a script that will download a file from user input, resize it to a thumbnail and add two new file names to the database.

However, I can’t understand for life how to get PHP to detect a MIME type image and then pass it to the header. Here is the code, I put comments to try to make it as clear as possible:

$picture = $_FILES['picture']['name']; /*original file location*/ $file = 'picture/'.$picture.''; /*save thumbnail location*/ $save = 'thumb/tn-'.$picture.''; /*append thumbnail filename with tn-*/ $thumb = 'tn-'.$picture.''; /*get original file size*/ list($width, $height) = getimagesize($file); /*get image MIME type*/ $size = getimagesize($file); $fp = fopen($file, "r"); if ($size && $fp) { header("Content-type:".$size['mime']); fpassthru($fp); exit; } else { echo 'Error getting filetype.'; } /*define thumbnail dimensions*/ $modwidth = 300; $modheight = 200; /*check to see if original file is not too small*/ if (($width > 301) || ($height > 201)) { /*create shell for the thumbnail*/ $tn = imagecreatetruecolor($modwidth, $modheight); /*HERE IS WHERE I HAVE TROUBLE*/ /*if MIME type is PNG*/ if ($size['mime'] == "image/png") { /*try to create PNG thumbnail*/ $image = imagecreatefrompng($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagepng($tn, $save, 100); } /*if MIME type is JPEG*/ if ($size['mime'] == "image/jpeg") { $image = imagecreatefromjpeg($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagejpeg($tn, $save, 100); } /*if MIME type is GIF*/ if ($size['mime'] == "image/gif") { $image = imagecreatefromgif($file); imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagegif($tn, $save, 100); } } else { echo 'Your file is too small.'; } 

So, here is the part that I don’t understand: the code works fine when I load .jpeg, but if it is PNG or GIF, it displays a page with the image saying 127.0. 0.1: 8888 'is not displayed because it contains errors.' I assume that it should not have the correct header type of the header, but maybe something else?

When I select .jpeg, it automatically loads into my image folder and generates a thumbnail in the Thumbnail folder as I want it.

But as soon as I try it with PNG and GIF, it fails. Should I miss something obvious?

Is this code good for what I'm trying to execute?

Thanks in advance,

+4
source share
2 answers

Using the PHP GD extension can really be a pain. Personally, I would recommend using an abstraction library like WideImage . This will greatly simplify your code:

 $picture = $_FILES['picture']['name']; /*original file location*/ $file = 'picture/'.$picture; move_uploaded_file($_FILES["userfile"]["tmp_name"], $picture); /*save thumbnail location*/ $save = 'thumb/tn-'.$picture; // Magic Begins Here ...... require "path-to/WideImage.php"; $image = WideImage::load($picture); $thumb = $image->resizeDown(300, 200, 'inside'); $thumb->saveToFile($save); 

Yes, all of this code above has been reduced to four bands.


A few pointers:

  • Never rely on $_FILES[]['mime'] . This has been submitted by the user agent and cannot be trusted.
  • For the same reason, I will not base my file names on $_FILES[]['name'] .

In addition, your source code does not work because you never move_uploaded_file() .

+6
source

Get the best solution. Images with images can actually be fun using the Thumbnailer class.

 function callb(& $image) { $image->thumbFixed(300,200)->save('/some/location/'.$image->filename); } // myFile refers to $_FILES['myFile'] // upload image and handle it Thumbnailer::upload('myFile', 'callb'); 
0
source

Source: https://habr.com/ru/post/1343098/


All Articles