Image Resizing class in PHP

<?php
Class Geo_Img_Resize {
 
    private $_image = null;
 
 
    function __construct($originalPath, $maxDim) {
        $this->process($originalPath, $maxDim);
    }
 
    private function process($path, $max) {
        $imageContent = file_get_contents($path);
        $image = imagecreatefromstring($imageContent);
 
        if ($image === false) {
            // Image corrupted
            return;
        }
 
        $imageHeight = imagesy($image);
        $imageWidth = imagesx($image);
 
        $newDim = null;
 
        if ($imageHeight > $imageWidth) {
            // Potrait
            $newDim = array(
                'height' => (int) $max,
                'width' =>  round($imageWidth * $max / $imageHeight)
            );
        } else {
            // Landscape
            $newDim = array(
                'height' => round($imageHeight * $max / $imageWidth),
                'width' => $max
            );
        }
 
        $newImage = imageCreateTrueColor($newDim['width'], $newDim['height']);
 
        imagecopyresized(
            $newImage, $image, 0,0,0,0,$newDim['width'],
            $newDim['height'],$imageWidth,$imageHeight
        );
 
        $this->_image = $newImage;
    }
 
    public function save($path) {
        if ($this->_image !== null) {
            imagepng($this->_image, $path);
        }
    }
 
    public function display() {
        imagepng($this->_image);
    }
}
?>

Download this snippet

Twitter Twitter

4 Comments to “ Image Resizing class in PHP”

  1. Anonymous  on Nov 04, 2009

    S0R965 <a href="http://hnwxxfohptrd.com/">hnwxxfohptrd</a>, [url=http://rjjizjqzvofv.com/]rjjizjqzvofv[/url], [link=http://xnokctffmrsi.com/]xnokctffmrsi[/link], http://phomfjrasxpl.com/

  2. Anonymous  on Dec 28, 2009

    sboOXb <a href="http://lvrcvaptpfdt.com/">lvrcvaptpfdt</a>, [url=http://sjgcqlpyfvxw.com/]sjgcqlpyfvxw[/url], [link=http://opuoqolaethm.com/]opuoqolaethm[/link], http://vudfeotfydjw.com/

  3. Anonymous  on Jan 13, 2010

    76Zjhj <a href="http://tsiqybcrffqg.com/">tsiqybcrffqg</a>, [url=http://yhxogynxzckp.com/]yhxogynxzckp[/url], [link=http://ahjgifgbwerj.com/]ahjgifgbwerj[/link], http://mybpuupeqthy.com/

  4. Anonymous  on Mar 14, 2010

    aKwygy <a href="http://fqlirzjefmwx.com/">fqlirzjefmwx</a>, [url=http://sgekjgnomuwv.com/]sgekjgnomuwv[/url], [link=http://cbmyusjlhqyu.com/]cbmyusjlhqyu[/link], http://ulgsxjischtr.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>