Return to Snippet

Revision: 28697
at July 13, 2010 18:17 by alessio2


Updated Code
/**
   * Function that creates thumbnail from image
   * Original author: Christian Heilmann
   * Fixed and extended: Ales Rebec
   *
   * @param $name       Original filename  (fullpath to image)
   * @param $filename   Filename of the resized image (fullpath to thumbnail that will be created)
   * @param $new_w      width of resized image in px (ex. 80 or 100)
   * @param $new_h      height of resized image in px
   */
  public function createthumb($name,$filename,$new_w,$new_h)
  {
          $type = 0;
          $size = getimagesize($name);
          $mime = $size['mime'];                    //get image mime type (ex. "image/jpeg" or "image/gif")
          $system=explode("/",$mime);
          if (preg_match("/jpg|jpeg/i",$system[sizeof($system)-1])) {$type=1; $src_img=imagecreatefromjpeg($name);}
          if (preg_match("/png/i",$system[sizeof($system)-1]))      {$type=2; $src_img=imagecreatefrompng($name);}
          if (preg_match("/gif/i",$system[sizeof($system)-1]))      {$type=3; $src_img=imagecreatefromgif($name);}
          $old_x=imageSX($src_img);
          $old_y=imageSY($src_img);
          if ($old_x > $old_y)                            //calculate thumnails dimenstions and preserve aspect ratio
          {
                  $thumb_w=$new_w;
                  $thumb_h=$old_y*($new_h/$old_x);
          }
          if ($old_x < $old_y) 
          {
                  $thumb_w=$old_x*($new_w/$old_y);
                  $thumb_h=$new_h;
          }
          if ($old_x == $old_y) 
          {
                  $thumb_w=$new_w;
                  $thumb_h=$new_h;
          }
          $dst_img= imagecreatetruecolor($thumb_w,$thumb_h);          //create new image "canvas"
          if($type > 1) $this->setTransparency($dst_img, $src_img);  //if GIF or PNG -> set tranparency
          imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
          if      ($type == 3)  imagegif($dst_img,$filename); 
          else if ($type == 2)  imagepng($dst_img,$filename); 
          else                  imagejpeg($dst_img,$filename); 
          imagedestroy($dst_img); 
          imagedestroy($src_img); 
  }

Revision: 28696
at July 12, 2010 23:06 by alessio2


Initial Code
/**
   * Function that creates thumbnail from image
   * Original author: Christian Heilmann
   * Fixed and extended: Ales Rebec
   *
   * @param $name       Original filename  (fullpath to image)
   * @param $filename   Filename of the resized image (fullpath to thumbnail tha will be created)
   * @param $new_w      width of resized image in px (ex. 80 or 100)
   * @param $new_h      height of resized image in px
   */
  public function createthumb($name,$filename,$new_w,$new_h)
  {
          $type = 0;
          $size = getimagesize($name);
          $mime = $size['mime'];                    //get image mime type (ex. "image/jpeg" or "image/gif")
          $system=explode("/",$mime);
          if (preg_match("/jpg|jpeg/i",$system[sizeof($system)-1])) {$type=1; $src_img=imagecreatefromjpeg($name);}
          if (preg_match("/png/i",$system[sizeof($system)-1]))      {$type=2; $src_img=imagecreatefrompng($name);}
          if (preg_match("/gif/i",$system[sizeof($system)-1]))      {$type=3; $src_img=imagecreatefromgif($name);}
          $old_x=imageSX($src_img);
          $old_y=imageSY($src_img);
          if ($old_x > $old_y)                            //calculate thumnails dimenstions and preserve aspect ratio
          {
                  $thumb_w=$new_w;
                  $thumb_h=$old_y*($new_h/$old_x);
          }
          if ($old_x < $old_y) 
          {
                  $thumb_w=$old_x*($new_w/$old_y);
                  $thumb_h=$new_h;
          }
          if ($old_x == $old_y) 
          {
                  $thumb_w=$new_w;
                  $thumb_h=$new_h;
          }
          $dst_img= imagecreatetruecolor($thumb_w,$thumb_h);          //create new image "canvas"
          if($type > 1) $this->setTransparency($dst_img, $src_img);  //if GIF or PNG -> set tranparency
          imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
          if      ($type == 3)  imagegif($dst_img,$filename); 
          else if ($type == 2)  imagepng($dst_img,$filename); 
          else                  imagejpeg($dst_img,$filename); 
          imagedestroy($dst_img); 
          imagedestroy($src_img); 
  }

Initial URL


Initial Description
* Creates thumbnail from image
  * thumbnail dimensions can be set as parameters
  * accepts JPG, JPEG, GIF, PNG
  * preserves transparency for GIF and PNG on thumbnails
  * call function like this: createthumb(images/original.jpg, thumbs/thumb.jpg, 80, 80);

Initial Title
Generate thumbnails with PHP by AR

Initial Tags
image

Initial Language
PHP