Advertisement
Guest User

function.php

a guest
Feb 28th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. /**
  2.  * Image resize
  3.  * @param int $width
  4.  * @param int $height
  5.  */
  6. function resize($width, $height){
  7.   /* Get original image x y*/
  8.   list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
  9.   /* calculate new image size with ratio */
  10.   $ratio = max($width/$w, $height/$h);
  11.   $h = ceil($height / $ratio);
  12.   $x = ($w - $width / $ratio) / 2;
  13.   $w = ceil($width / $ratio);
  14.   /* new file name */
  15.   $path = 'uploads/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
  16.   /* read binary data from image file */
  17.   $imgString = file_get_contents($_FILES['image']['tmp_name']);
  18.   /* create image from string */
  19.   $image = imagecreatefromstring($imgString);
  20.   $tmp = imagecreatetruecolor($width, $height);
  21.   imagecopyresampled($tmp, $image,
  22.     0, 0,
  23.     $x, 0,
  24.     $width, $height,
  25.     $w, $h);
  26.   /* Save image */
  27.   switch ($_FILES['image']['type']) {
  28.     case 'image/jpeg':
  29.       imagejpeg($tmp, $path, 100);
  30.       break;
  31.     case 'image/png':
  32.       imagepng($tmp, $path, 0);
  33.       break;
  34.     case 'image/gif':
  35.       imagegif($tmp, $path);
  36.       break;
  37.     default:
  38.       exit;
  39.       break;
  40.   }
  41.   return $path;
  42.   /* cleanup memory */
  43.   imagedestroy($image);
  44.   imagedestroy($tmp);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement