SimpleImage::resize PHP Method

resize() public method

Resize an image to the specified dimensions
public resize ( integer $width, integer $height ) : SimpleImage
$width integer
$height integer
return SimpleImage
    function resize($width, $height)
    {
        // Generate new GD image
        $new = imagecreatetruecolor($width, $height);
        if ($this->original_info['format'] === 'gif') {
            // Preserve transparency in GIFs
            $transparent_index = imagecolortransparent($this->image);
            if ($transparent_index >= 0) {
                $transparent_color = imagecolorsforindex($this->image, $transparent_index);
                $transparent_index = imagecolorallocate($new, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                imagefill($new, 0, 0, $transparent_index);
                imagecolortransparent($new, $transparent_index);
            }
        } else {
            // Preserve transparency in PNGs (benign for JPEGs)
            imagealphablending($new, false);
            imagesavealpha($new, true);
        }
        // Resize
        imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
        // Update meta data
        $this->width = $width;
        $this->height = $height;
        $this->image = $new;
        return $this;
    }

Usage Example

 function imageDualResize($filename, $cleanFilename, $wtarget, $htarget)
 {
     if (!file_exists($cleanFilename)) {
         $dims = getimagesize($filename);
         $width = $dims[0];
         $height = $dims[1];
         while ($width > $wtarget || $height > $htarget) {
             if ($width > $wtarget) {
                 $percentage = $wtarget / $width;
             }
             if ($height > $htarget) {
                 $percentage = $htarget / $height;
             }
             /*if($width > $height)
             		{
             			$percentage = ($target / $width);
             		}
             		else
             		{
             			$percentage = ($target / $height);
             		}*/
             //gets the new value and applies the percentage, then rounds the value
             $width = round($width * $percentage);
             $height = round($height * $percentage);
         }
         $image = new SimpleImage();
         $image->load($filename);
         $image->resize($width, $height);
         $image->save($cleanFilename);
         $image = null;
     }
     //returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the
     return "src=\"{$baseurl}/{$cleanFilename}\"";
 }
All Usage Examples Of SimpleImage::resize