Advertisement
Sixem

PHP Resize Image - Keep Dimensions

Jun 24th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3.  
  4. $image_size = stripslashes($_GET['s']);
  5. $image_source = stripslashes($_GET['f']);
  6. $image_extension = stripslashes($_GET['x']);
  7.   if ($image_extension == 'png') {
  8.     $src = imagecreatefrompng($image_source);
  9.   }
  10.   if ($image_extension == 'jpg') {
  11.     $src = imagecreatefromjpeg($image_source);
  12.   }
  13.   if ($image_extension == 'jpeg') {
  14.     $src = imagecreatefromjpeg($image_source);
  15.   }
  16. $dw = $image_size;
  17. $dh = $image_size;
  18. $sw = imagesx($src);
  19. $sh = imagesy($src);
  20. $wr = $sw / $dw;
  21. $hr = $sh / $dh;
  22. $cx = 0;
  23. $cy = 0;
  24. if ($hr < $wr)
  25. {
  26.     $ow = $sw;
  27.     $sw = $dw * $hr;
  28.     $cx = ($ow - $sw) / 2;
  29. }
  30. if ($wr < $hr)
  31. {
  32.     $oh = $sh;
  33.     $sh = $dh * $wr;
  34.     $cy = ($oh - $sh) / 2;
  35. }
  36. $dst = imagecreatetruecolor($dw, $dh);
  37. imagecopyresampled($dst, $src, 0, 0, $cx, $cy, $dw, $dh, $sw, $sh);
  38. header('Content-type: image/png');
  39. imagepng($dst);
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement