Welcome Guest, Not a member yet? Register   Sign In
Image Resizing Questions/Problems
#1

[eluser]Paul Skinner[/eluser]
I am very new to CodeIgniter, and not quite so new (but still fairly new) to PHP, so please be extra lovely and explain things so that I can learn for next time.

The standard code for resizing images using CI is below.
This is highly useful for what I need, however I am stuck as to how to add the extra functionality that I require.

Background on what I need:

I need to be able to resize up to 50 (unlikely it'll ever actually reach 50 in one go) images in to two smaller versions of the original, and move the original to a different location (i.e. copy to a different location and delete the old one).
The script should be able to read all the pictures in a particular directory like the one below and do the conversion. It is not necessary for the script to read sub-directories.

domain.com/admin/uploaded/photos/
(which contains image1.jpg, image2.jpg, image3.jpg etc).

Once the conversion is complete I should end up with these URLs, for example.

domain.com/pictures/original/image1.jpg
domain.com/pictures/medium/image1.jpg
domain.com/pictures/smaller/image1.jpg


and the URL domain.com/admin/uploaded/photos/ should be empty (ready for when the next set of photos are uploaded).

The method of resizing that is supplied below is fine (where an ideal size is set and CI tries to resize it as close as possible while keeping the aspect ratio.

The script does not need to randomise the file name.

It would be extra lovely if it could output a message when conversion is complete, but that may be more hassle than is worth it.


Thank you in advance for any help you give.

Paul

Code:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;

$this->load->library('image_lib', $config);

$this->image_lib->resize();
#2

[eluser]Randy Casburn[/eluser]
Hi Paul,

Are you actually asking for someone to write this code for you? How about putting up the code that you've written already, that you've tried but didn't work. Labored over, struggled with, and just could quite get over that last hurdle...

Post up that code, bet someone will be along to help you fix it right up.
#3

[eluser]Paul Skinner[/eluser]
[quote author="Randy Casburn" date="1215662518"]Hi Paul,

Are you actually asking for someone to write this code for you? How about putting up the code that you've written already, that you've tried but didn't work. Labored over, struggled with, and just could quite get over that last hurdle...

Post up that code, bet someone will be along to help you fix it right up.[/quote]

No problem. I will do that in the morning as it's getting late here now.
I have no idea whatsoever as to how to get it to apply to all the pictures in a directory (oh yes, that's the other thing, there's only going to be pictures in that directory; nothing else - that may make it easier, I don't know), but have a vague idea how to get it to do the multiple resizings.

I'll post my attempt in the morning.


Paul
#4

[eluser]ontguy[/eluser]
The file helper could help with what you want to get done:
Code:
$this->load->helper('file');
get_filenames('path/to/directory/')
http://ellislab.com/codeigniter/user-gui...elper.html

The php command "unlink" when you want to delete a file.
#5

[eluser]Paul Skinner[/eluser]
Right. I have this code now:

Code:
<?php
class Test extends Controller {

function imageresize(){

$this->load->helper('file');
$this->load->library('image_lib');

foreach(get_filenames('pics/', true) as $f)
    {

    $config['image_library'] = 'gd2';
    $config['source_image'] = $f;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 475;
    $config['height'] = 450;
    $config['new_image'] = 'pics/large/';
    $config['master_dim'] = 'height';
    $config['quality'] = '70%';
    
    $this->image_lib->initialize($config);
    
    $this->image_lib->resize();

    }
    
    
foreach(get_filenames('pics/', true) as $f)
    {

    $config['image_library'] = 'gd2';
    $config['source_image'] = $f;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 300;
    $config['height'] = 250;
    $config['new_image'] = 'pics/medium/';
    $config['master_dim'] = 'height';
    $config['quality'] = '70%';
    
    $this->image_lib->initialize($config);
    
    $this->image_lib->resize();

    }    
    
    
    
foreach(get_filenames('pics/', true) as $f)
    {

    $config['image_library'] = 'gd2';
    $config['source_image'] = $f;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 200;
    $config['height'] = 100;
    $config['new_image'] = 'pics/small/';
    $config['master_dim'] = 'height';
    $config['quality'] = '70%';
    
    $this->image_lib->initialize($config);
    
    $this->image_lib->resize();


    }
    
foreach(get_filenames('pics/', true) as $f)
    {
    unlink($f);
    }
    
    
    
    echo 'Job Done';

}
}

?>

This is (semi) working, but unfortunately times-out before it completes with 7 files, let alone up to the 50 that I need.
The other problem is that my unlink command seems to delete all the newly created resized pictures as well as the originals (or the new pictures aren't permanently stored?).



The error I get is:

"Fatal error: Maximum execution time of 60 seconds exceeded in C:\Program Files\XAMPP\htdocs\code\system\libraries\Image_lib.php on line 524"

There is no obvious way to set this limit higher as line 524 doesn't contain any timing data.

If anyone knows how to fix that, please let me know.
#6

[eluser]Randy Casburn[/eluser]
The 60 second time out is a function of your PHP interpreter. That setting is found in your PHP ini file. I'm not sure changing that setting is necessarily the optimal thing to do. The reason is that you may run into deployment issues later on down the road if you need to move this application to a server that you have no span of control over.

There may be other ways to solve this. For instance, it may be an option to attempt to optimize the design of your software rather than changing your infrastructure to accommodate ONE software class of thousands that reside happily within this very same infrastructure.

Does this make sense?

Randy
#7

[eluser]Paul Skinner[/eluser]
It makes sense, yes.

It's a shame it's part of the PHP.ini file. Is there any workaround that you would suggest (other than the user doing the resizing on their computer as opposed to the server)?
#8

[eluser]Randy Casburn[/eluser]
OK. Try it this way. You're likely to have to compromise on the setting. Most image processing systems have to budge the timeout setting a little if they are processing very large images or insist on processing very large numbers of them all at once.

If there is no way to break up your batch into smaller chunks for processing, then let's at least make things a little more efficient. let's start with this.

Try this. Your script will still time out. I would like you to see what I've done with your code. Note the only two items left "hard coded" in the class are the "70%" quality level and the master dimension of the image. I thought maybe you could pull those out yourself so there was nothing left "hard coded" in your class. In the future if you decide you want the dimensions of the thumbnails (or whatever) to be larger all you have to do is send in different proportions rather than hack to class.

You need to put in some error checking too.

Code:
<?php
$myTest = new Test();

$myTest->largeWidth['width'] = 475;
$myTest->largeHeight['height'] = 450;
$myTest->largeImageDir['dir'] = '/pics/large';
$myTest->mediumWidth['width'] = 300;
$myTest->mediumHeight['height'] = 20;
$myTest->mediumImageDir['dir'] = '/pics/medium';
$myTest->smallWidth['width'] = 200;
$myTest->smallHeight['height'] = 100;
$myTest->smallImageDir['dir'] = '/pics/small';


class Test extends Controller {

    var $config;
    var $largeWidth;
    var $largeHeight;
    var $largeImageDir;
    var $mediumWidth;
    var $mediumImageDir;
    var $smallWidth;
    var $smallHeight;
    var $smallImageDir;

    function Test(){
        $this->__construct();
    }


    function __construct(){
        $this->config['image_library'] = 'gd2';
        $this->config['source_image'] = $f;
        $this->config['create_thumb'] = FALSE;
        $this->config['maintain_ratio'] = TRUE;
        $this->config['master_dim'] = 'height';
        $this->config['quality'] = '70%';
    }
        
    function imageresize(){

        $this->load->helper('file');
        $this->load->library('image_lib');

        foreach(get_filenames('pics/', true) as $f)
            {

            $this->config['width'] = $this->largeWidth;
            $config['height'] = $this->largeHeight;
            $config['new_image'] = $this->largeImageDir;
            $this->image_lib->initialize($this->config);// <- EDIT

            $this->image_lib->resize();

            $this->config['width'] = $this->mediumWidth;
            $config['height'] = $this->mediumHeight;
            $config['new_image'] = $this->mediumImageDir;
            $this->image_lib->initialize($this->config);// <- EDIT

            $this->image_lib->resize();
            
            $this->config['width'] = $this->smallWidth;
            $config['height'] = $this->smallHeight;
            $config['new_image'] = $this->smallImageDir;
            $this->image_lib->initialize($this->config);// <- EDIT
            $this->image_lib->resize();

        /* You need to ensure you have your resized images befor you delete
           your original */    
            unlink($f);
            }

            echo 'Job Done';

    }
}
?&gt;

Have fun.

Randy
#9

[eluser]Paul Skinner[/eluser]
Thanks very much for the help.
I'll give your example a test and see if it'll do enough for what I need.
#10

[eluser]Randy Casburn[/eluser]
Please note the edits to the previous post. I forgot the put $this->config in the calls to the image lib function.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB