All Image to JPG/JPeG Converter and Resizer


/ Published in: PHP
Save to your folder(s)

I edit this from tylerhall on 11/30/-1


Copy this code and paste it in your HTML
  1. function image($file_temporary, $file_destination, $width, $height, $type = "jpg") {
  2. $type = strtolower($type);
  3. $image_size = getimagesize($file_temporary);
  4.  
  5. if($image_size[0] > $image_size[1]) {
  6. $new_width = $width;
  7. $new_height = $image_size[1] * ($new_width / $image_size[0]);
  8. }
  9. else {
  10. $new_height = $height;
  11. $new_width = $image_size[0] * ($new_height / $image_size[1]);
  12. }
  13.  
  14. switch(image_type_to_mime_type($image_size[2])) {
  15. case "image/jpeg":
  16. $image = imagecreatefromjpeg($file_temporary);
  17. break;
  18. case "image/gif":
  19. $image = imagecreatefromgif($file_temporary);
  20. break;
  21. case "image/png":
  22. $image = imagecreatefrompng($file_temporary);
  23. break;
  24. default:
  25. $t = image_type_to_mime_type($image_size[2]);
  26. echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
  27. break;
  28. }
  29.  
  30. $thumb = imagecreatetruecolor($new_width, $new_height);
  31. imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $image_size[0], $image_size[1]);
  32.  
  33. switch($type) {
  34. case "jpg":
  35. case "jpeg":
  36. //header("Content-type: image/jpeg");
  37. imagejpeg($thumb, $file_destination);
  38. break;
  39. case "gif":
  40. //header("Content-type: image/gif");
  41. imagegif($thumb, $file_destination);
  42. break;
  43. case "png":
  44. //header("Content-type: image/png");
  45. imagepng($thumb, $file_destination);
  46. break;
  47. default:
  48. echo "The image type {$type} is not supported, please choose another.";
  49. break;
  50. }
  51.  
  52. imagedestroy($image);
  53. imagedestroy($thumb);
  54.  
  55. return true;
  56. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.