Advertisement
Guest User

Untitled

a guest
Jun 9th, 2010
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <?php
  2.  
  3. resize_image('myImg.png', 150, 150);
  4.  
  5. function resize_image($src_name, $w, $h)
  6. {
  7.  
  8.     $src = imagecreatefrompng($src_name);
  9.    
  10.     // Getting som imageinfo
  11.     list($width, $height, $image_type) = getimagesize($src_name);
  12.  
  13.     // keeping the ratio
  14.     if($height > $width)    {
  15.         $ratio = $h / $height;
  16.        
  17.         $new_width = $width * $ratio;
  18.         $new_height = $height * $ratio;
  19.     }
  20.     else {
  21.         $ratio = $w / $width;
  22.        
  23.         $new_width = $width * $ratio;
  24.         $new_height = $height * $ratio;
  25.     }
  26.    
  27.     // Creates a temp-image
  28.     $temp = imagecreatetruecolor($new_width, $new_height);
  29.     // Stängar av alphablendnig för att kunna spara alphakanalen
  30.     imagealphablending($temp, false);
  31.    
  32.     // Fixar filnamn
  33.     $newName = explode('.', $src_name);
  34.     $newName = 'images/'.$newName[0];
  35.     $newName .= '.png'; // Saves png
  36.    
  37.     // resizing
  38.     imagecopyresampled($temp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  39.     // Activates the alpha-channel
  40.     imagesavealpha($temp, true);
  41.     // Saves the image
  42.     imagepng($temp, $newName);
  43.    
  44.     // destroy the objects
  45.     imagedestroy($src);
  46.     imagedestroy($temp);
  47. }
  48.  
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement