Welcome Guest, Not a member yet? Register   Sign In
Somehow image thumbnailing doesn't work as espected
#1

[eluser]Fabdrol[/eluser]
Hi folks!

I'm using the following methods to resize my image into an thumbnail. Somehow, it doesn't work as inspected. In stead of saving a file, it outputs the original image to the browser..

Can't get really much feedback either, no way to catch error messages or anything..

Code:
function _createthumb($img, $w) {
            $original = $this->_getimage($img->mimetype, $img->fs_name);
            $data['thumbnail'] = uid();
            $filename = './uploads/blobs/'.$data['thumbnail'];
            $table = $this->config->item('bijlagen');
        
            list($width, $height) = getimagesize($original);
            
            $xscale = $width / $w;
            $yscale = $height / $w;
            
            if($yscale > $xscale){
                $new_width = round($width * (1 / $yscale));
                $new_height = round($height * (1 / $yscale));
            } else {
                $new_width = round($width * (1 / $xscale));
                $new_height = round($height * (1 / $xscale));
            }
        
            switch($img->mimetype) {
                   case 'image/jpeg':
                       $resized = imagecreatetruecolor($new_width, $new_height);
                       $temporary = imagecreatefromjpeg($original);
                      
                    imagecopyresampled($resized, $temporary, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagejpeg($resized, $filename, 100);
                   break;
                  
                   case 'image/png':
                       $resized = imagecreatetruecolor($new_width, $new_height);
                       $temporary = imagecreatefrompng($original);
                      
                    imagecopyresampled($resized, $temporary, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagepng($resized, $filename, 100);
                   break;
               }
              
               $this->db->where('uid', $img->uid);
            $this->db->update($table, $data);
            
            return $r;
        }
#2

[eluser]Fabdrol[/eluser]
I tried this as well.. but that only seems to work the first time I use it!
Any thoughts?

Code:
function _create_thumb($img, $wh) {
            $data['thumbnail'] = uid();
            $table = $this->config->item('bijlagen');
            
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/blobs/'.$img->fs_name;
            $config['new_image'] = $data['thumbnail'];
            $config['maintain_ratio'] = TRUE;
            $config['width'] = $wh;
            $config['height'] = $wh;
            
            $this->load->library('image_lib', $config);
            
            if(!$this->image_lib->resize()) {
                echo "<pre>";
                echo date("H:m:i")."  Path: ".$config['source_image']."\n";
                echo $this->image_lib->display_errors(date("H:m:i")."  ", "\n");
                echo "</pre>";
            }
            
            $this->db->where('uid', $img->uid);
            $this->db->update($table, $data);
            
            $this->_getimage($img->mimetype, $data['thumbnail']);
            $this->image_lib->clear();
        }
#3

[eluser]Fabdrol[/eluser]
I've found out something:

When resizing fails (or saving of the resized image, i'm not sure) the original image is shown. The code to retrieve the thumbnail
Code:
$this->_getimage($img->mimetype, $data['thumbnail']);
is not run.

When I run the code again to retrieve the thumbnail, after that, a url to the image is shown, not the thumbnail. That's because there's no thumbnail in the File system.

The code to retrieve the thumbnail:

Code:
function _getimage($mime, $fs) {
    header("Content-type: ".$mime);
    echo file_get_contents(base_url() . 'uploads/blobs/'.$fs);
}
#4

[eluser]Fabdrol[/eluser]
One more thing: all my images are saved as files without filenames.
Is that a big problem?

Example filename:
Code:
uploads/blobs/8a96d93d2d69fa509ecfef474f433451

Cheers,
Fabian
#5

[eluser]Fabdrol[/eluser]
strange.. some thumbnails are actually saved.. although not consequently...
strange strange strange!
#6

[eluser]WebsiteDuck[/eluser]
Just wondering, is uid() your own function? Is it similar to PHP's uniqid()?
#7

[eluser]Fabdrol[/eluser]
@bkaxrf

yeah, it's a helper I use to generate even more unique ID's.
Why?
#8

[eluser]Fabdrol[/eluser]
Hi folks, already got it working.
When I tried to do the trick with CI's image lib, I forgot to point to that function, somewhere in the script.
pretty stupid, eh?

Well, thanks anyway.

Oh jeh, @bkaxrf, if u are interested.. my UID helper works like this:
1. Generate an ultra, super, duper, deluxe unique string
2. Salt it with some time() and a bit of my own spit
3. SHA1 the whole package for portability
4. Check a database table if it doesn't exist
5. Store it in a database table
6. Return the UID()

May sound like a bit over the top, but I like my ID pointers to be hard to guess. The store&check;functionality is configurable, so in small apps I turn in off. (most of the time, the uid method is quite watertight)

Cheers!
Fabian
#9

[eluser]WebsiteDuck[/eluser]
Nevermind :lol:
#10

[eluser]Fabdrol[/eluser]
Well, it outputs the "large" image. thought I fixed it though...




Theme © iAndrew 2016 - Forum software by © MyBB