Jump to content

Image + Php


ckrudelux
 Share

Recommended Posts

Need some help I want to resize images for example I have a image I want to upload that is 180px * 180px but I want it to be 80px * 80px then I save it to the server image directory.

Link to comment
Share on other sites

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {    return;  }  if ($w == 0 && $h == 0)  {    return;  }  if ($dest == '')    $dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {    case 'jpg':    case 'jpeg':      $i = imagecreatefromjpeg($src);    break;    case 'gif':      $i = imagecreatefromgif($src);    break;    case 'png':      $i = imagecreatefrompng($src);    break;    default:      return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w < $w && $h == 0) ||      ($w == 0 && $h != 0 && $new_h < $h) ||      ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h))  {    // image is small enough    if ($dest != $src)      copy($src, $dest);    return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {    $new_h = ($w / $new_w) * $new_h;    $new_w = $w;  }  if ($h != 0 && $new_h > $h)  {    $new_w = ($h / $new_h) * $new_w;    $new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {    case 'jpg':    case 'jpeg':      imagejpeg($new, $dest);    break;    case 'gif':      imagegif($new, $dest);    break;    case 'png':      imagepng($new, $dest);    break;  }  imagedestroy($new);}

ex:

resize_image(array(  'source' => '/path/to/source.jpg',  'dest' => '/path/to/save.jpg', // optional, leave off to overwrite source  'w' => 80,  'h' => 80));

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...