Image resize function at CakePHP 2.0



    //How to call another function
    public function imgresize(){
        //create thumbnail of original image
        $source_image = WWW_ROOT . 'upload_books/' . $new_image_name;
        $destination_thumb_path = WWW_ROOT . 'upload_books/thumbs/' . $new_image_name;
        $this->__imageresize($source_image, $destination_thumb_path, 92, 112);
    }
/*
     *  function name __imageresize(
     *     Image Source, which one's thumb image will be create,
     *     Destination thumbs path with image name,
     *     Image width size,
     *     Image height size
     *  )
     * Note: If original image width is grater than height then the width, height value will be swap
     *
     */

    public function __imageresize($imagePath, $thumb_path, $destinationWidth, $destinationHeight) {
        // The file has to exist to be resized
        if (file_exists($imagePath)) {
            // Gather some info about the image
            $imageInfo = getimagesize($imagePath);

            // Find the intial size of the image
            $sourceWidth = $imageInfo[0];
            $sourceHeight = $imageInfo[1];

            if ($sourceWidth > $sourceHeight) {
                $temp = $destinationWidth;
                $destinationWidth = $destinationHeight;
                $destinationHeight = $temp;
            }

            // Find the mime type of the image
            $mimeType = $imageInfo['mime'];

            // Create the destination for the new image
            $destination = imagecreatetruecolor($destinationWidth, $destinationHeight);

            // Now determine what kind of image it is and resize it appropriately
            if ($mimeType == 'image/jpeg' || $mimeType == 'image/jpg' || $mimeType == 'image/pjpeg') {
                $source = imagecreatefromjpeg($imagePath);
                imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
                imagejpeg($destination, $thumb_path);
            } else if ($mimeType == 'image/gif') {
                $source = imagecreatefromgif($imagePath);
                imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
                imagegif($destination, $thumb_path);
            } else if ($mimeType == 'image/png' || $mimeType == 'image/x-png') {
                $source = imagecreatefrompng($imagePath);
                imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
                imagepng($destination, $thumb_path);
            } else {
                $this->Session->setFlash(__('This image type is not supported.'), 'flash_error');
            }

            // Free up memory
            imagedestroy($source);
            imagedestroy($destination);
        }
    }


Advertisements

3 responses to “Image resize function at CakePHP 2.0

  1. Hi,

    Thanks for this thumbnail image code,

    but, .png images are generating thumbnail with the black background.

    please give me some solution for that.

    thanks in advance.

    Like

    • Do you want to upload and try to resize an png image with transparent background. Then this problem can arise.

      Recode the png condition section like below, Hope it will solve your problem insha allah.

      
         else if ($mimeType == 'image/png' || $mimeType == 'image/x-png') {
             $source = imagecreatefrompng($imagePath);
             imagealphablending( $destination, false );
             imagesavealpha( $destination, true );
             imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
             imagepng($destination, $thumb_path);                                 
         } 
      
      

      thanks
      ziyed

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s