Page 1 of 1

imagick works only on first try

Posted: Fri Dec 19, 2008 7:42 am
by Cgull
Hello,

This is the first time I'm trying to use imageMagick to create thumbnails.

I use a tutorial I found on the net, and this tutorial works only the first time I upload an image. If I try to upload an image again, it says that the thumbnail was created successfully, but actually, no thumbnail was created.
My code:

Code: Select all

<?php
    class thumb {
        private $file_url;
        private $max_width;
    
    public function setImage($file_url, $file_name, $max_width) {
        $this->file_url = $file_url;
        $this->file_name = $file_name;
        $this->max_width = $max_width;
    }
    
    public function getThumbUrl() {
        $current_file = $this->file_url . $this->file_name; 
        $current_size = getimagesize($current_file);
        $current_img_width = $current_size[0];
        $current_img_height = $current_size[1];
        $image_base = explode('.', $this->file_name);
            
        // This part gets the new thumbnail name
        $image_basename = $image_base[0];
        $image_ext = $image_base[1];
        $thumb_name = $this->file_url . "thumbs/" . $image_basename.'_thumb.'.$image_ext;
 
        if($current_img_width > $current_img_height) {
            $too_big_diff_ratio = $current_img_width/$this->max_width;
            $new_img_width = $this->max_width;
            $new_img_height = round($current_img_height/$too_big_diff_ratio);
        } else {
            $too_big_diff_ratio = $current_img_height/$this->max_width;
            $new_img_height = $this->max_width;
            $new_img_width = round($current_img_width/$too_big_diff_ratio);
 
        }
 
        // Convert the file
        $make_magick = system("c:\tools\ImageMagick\convert -geometry $new_img_width x $new_img_height $current_file $thumb_name", $retval);
      // Did it work?
      if (!($retval)) {
        echo 'Thumbnail created: <img src="' .$thumb_name .'">';
      }
      else {
        echo 'Error: Please try again.';
      }
 
 
        echo $new_img_width . " new width<br />";
        echo $new_img_height . " new height<br />";
        return($thumb_name);
    }
}
?>
Please help,

Thanks,

Cgull