Welcome Guest, Not a member yet? Register   Sign In
Crop images with Image Manipulation Class
#2

[eluser]waynhall[/eluser]
I recently was able to make some square thumbnails, but I found the ImageMagick class of PHP to work better than CodeIgniter in this instance.

Here is a function in my photos controller that I used to generate thumbnails for an entire directory. It doesn't involve any client-side customization; it just automatically generates the thumbnails, but maybe you can figure something out from the documentation at: http://php.net/manual/en/book.imagick.php

(Note that I'm saving pngs, not jpgs)
(You'll need imagemagick and php5-imagick (On Ubuntu: sudo apt-get install imagemagick php5-imagick)

Code:
private function _make_thumbs($dir) {
            // makes 75x75 .png thumbnail images in bulk for the root level of $dir
            // and places them inside a thumbs/ folder
            
            $img_dir = '/var/www/sleepin4/img/';
            $dir = $img_dir . $dir;
            
            // Add trailing slash if need be
            if(substr($dir, -1) != '/'){
                $dir = $dir . '/';
            }
            
            // Attempt to make a thumbs folder
            mkdir($dir . 'thumbs', 0755);
            
            $this->load->helper('directory');
            $map = directory_map($dir);
            
            foreach($map as $key => $value) {
                
                if(is_numeric($key) && !stripos($value, 'thumb')) {
                    
                    
                    $src = $dir . $value;
                    $target = $dir . 'thumbs/'. substr($value, 0, stripos($value, '.jpg')) . '.png';;


                    /* Read the image */
                    $im = new imagick( $src );
                    $im->cropThumbnailImage( 75, 75 );
                    $im->writeImage($target);
                    
                    $im->clear();
                    $im->destroy();

                }                
            }
        }


Messages In This Thread
Crop images with Image Manipulation Class - by El Forum - 07-27-2011, 08:06 AM
Crop images with Image Manipulation Class - by El Forum - 07-28-2011, 05:42 AM
Crop images with Image Manipulation Class - by El Forum - 07-28-2011, 07:08 AM
Crop images with Image Manipulation Class - by El Forum - 07-28-2011, 07:25 AM
Crop images with Image Manipulation Class - by El Forum - 07-28-2011, 07:41 AM
Crop images with Image Manipulation Class - by El Forum - 07-28-2011, 07:45 AM
Crop images with Image Manipulation Class - by El Forum - 02-28-2013, 08:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB