Welcome Guest, Not a member yet? Register   Sign In
Help understanding image_lib - resizing?!
#1

[eluser]gRoberts[/eluser]
Hi all,

Sorry to ask such a stupid question but I am having some major problems trying to understand the resize function of the image library.

Basically, the user uploads a single image, and I want to create a thumbnail (100x100) and I want to create a medium image (250x250).

The code below creates the 100x100 image but the image is TINY (2x2) inside a black square (100x100) and the 250x250 image remains the original size (its over 250x250).

Code:
function generateThumbnail($source, $output, $width, $height, $watermark = true) {
        $base =& get_instance();
        $dir = dirname($output);
        if(!is_dir($dir))
            recursive_mkdir($dir);
            
        copy($source, $output);
            
        $config['library_path'] = '';
        $config['source_image'] = $output;
        $config['maintain_ratio'] = TRUE;
        $config['quality'] = 100;
        $config['width'] = (int)$width;
        $config['height'] = (int)$height;
            
        $base->load->library('image_lib', $config);
        if(!$base->image_lib->resize())
            die($base->image_lib->display_errors());
        
        if($watermark)
            applyWatermarkToImage($output);    
        
    }

Without copying the file first, it doesn't do anything.

Any advice would be gratefully appreciated!

Cheers
#2

[eluser]tonanbarbarian[/eluser]
One of the strange characteristic I have noticed with the image library (and most of the core CI libraries that have config options) is that if you do not specify an option it resets to the default.

The reason you need to copy is you are not setting the new image config option, so it is trying to modify the original
You can either set the new image config option or use the create thumb option. Using create thumb will rename the file and put _thumb into the filename.

So here is what I would try
Code:
function generateThumbnail($source, $output, $width, $height, $watermark = true) {
        $base =& get_instance();
        $dir = dirname($output);
        if(!is_dir($dir))
            recursive_mkdir($dir);
            
        $config['library_path'] = '';
        $config['source_image'] = $source;
        $config['new_image'] = $source;
        $config['maintain_ratio'] = TRUE;
        $config['quality'] = 100;
        $config['width'] = (int)$width;
        $config['height'] = (int)$height;
            
        $base->load->library('image_lib', $config);
        if(!$base->image_lib->resize())
            die($base->image_lib->display_errors());
        
        if($watermark)
            applyWatermarkToImage($output);    
        
    }
I think this should do what you want. It should create a new image at the correct size. Without the new image all it was doing was resizing the image inside the old image thus the black border.

When working with this library my suggestion to to copy all of the config options listed in the user guide
http://ellislab.com/codeigniter/user-gui...e_lib.html
and set them as you think they should be until it displays as you expect, then remove those that you think are not needed until you have just the options that are needed to display correctly.
#3

[eluser]gRoberts[/eluser]
Hi, thanks for replying.

I used the original code, and i was originally using new_image as well but it was only copying the image.

The only time it would would semi-successfully is if I were to set create_thumb to true and not specify a new_image path. I also tried changing the _thumb part to "" but it caused the same problem.

I'll give that a go a little later on when I get to work.

Thanks again
#4

[eluser]gunter[/eluser]
donĀ“t use this code:
Code:
$base->load->library('image_lib', $config);
it works only once... because if the library is already loaded it will do nothing....

to use a new config array there is another function... cannot look now which one... have to go out to buy some food ;-)
#5

[eluser]gRoberts[/eluser]
I think we're onto a winner. I will try that asap. Using initialize instead. I'm actually going to write a class to deal with it.
#6

[eluser]gunter[/eluser]
okay, the food is in the house ;-)

use this only once..
Code:
$this->load->library('image_lib');


and this one for initializing your image lib arrays..:
Code:
$this->image_lib->initialize($config);
#7

[eluser]gRoberts[/eluser]
Wink whey. We're onto a winner. Now I have another problem. All my thumbs have the "_thumb" in the filename.

Code:
<?php

    /**
     * @author Gavin Roberts
     * @copyright 2007
     */
    
    class thumbGenerator {
                
        public $imageDir;
        private $base;
                
        public function __construct() {
            
            $this->base =& get_instance();
            $this->base->load->library('image_lib');
            
        }
        
        public function generate($source, $output, $width, $height) {
            
            $config['source_image'] = $source;
            $config['maintain_ratio'] = TRUE;
            $config['create_thumb'] = true;
            $config['new_image'] = $output;
            $config['quality'] = 100;
            $config['width'] = (int)$width;
            $config['height'] = (int)$height;
            
            $this->base->image_lib->initialize($config);
            if(!$this->base->image_lib->resize())
                die($this->base->image_lib->display_errors());
            
            $tempOutput = $this->generateThumbName($output);
            chmod($tempOutput, 0777);
            rename($tempOutput, $output);
            
        }
        
        private function generateThumbName($name) {
            
            $pi = pathinfo($name);
            return dirname($name) . '/' . $pi['filename'] . '_thumb.' . $pi['extension'];
            
        }
        
        public function applyWatermark($to, $watermark) {
            
            $config['source_image'] = $to;
            $config['wm_type'] = 'overlay';
            $config['quality'] = 100;
            $config['wm_overlay_path'] = $watermark;
            $config['wm_opacity'] = 50;
        
            $this->base->image_lib->initialize($config);
            return $this->base->image_lib->watermark();
            
        }
        
    }

?>

Thats my code.

Whats annoying me is that rename is actually creating a copy of the file and not renaming it.

So is there any way to explicitly set the new filename when creating a thumbnail?

Cheers again!
#8

[eluser]tonanbarbarian[/eluser]
either remove the config option for creating the thumb or set it to false
Code:
$config['create_thumb'] = false;
from what I understand if you are setting new_image you do not need create_thumb

The way it is supposed to work is this

if create_thumb is set then it creates a new image named [sourceimage]_thumb.ext
otherwise if new_image is set it creates a new image with the specified name
finally if neither is set it modifies the source_image
#9

[eluser]gRoberts[/eluser]
you know what, that didn't work last time!

Thank you again!




Theme © iAndrew 2016 - Forum software by © MyBB