SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Jmz's Avatar
    Join Date
    Jun 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating thumbnails

    I'm using the function from this tutorial to generate image thumbnails.

    My function looks like this:
    PHP Code:
    /*
        Function createthumb($name,$filename,$new_w,$new_h)
        creates a resized image
        variables:
        $name        Original filename
        $filename    Filename of the resized image
        $new_w        width of resized image
        $new_h        height of resized image
    */    
    function createthumb($name,$filename,$new_w,$new_h)
    {
        
    $system=explode(".",$name);
        if (
    preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
        if (
    preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
        
    $old_x=imageSX($src_img);
        
    $old_y=imageSY($src_img);
        if (
    $old_x $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$old_y*($new_h/$old_x);
        }
        if (
    $old_x $old_y
        {
            
    $thumb_w=$old_x*($new_w/$old_y);
            
    $thumb_h=$new_h;
        }
        if (
    $old_x == $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$new_h;
        }
        
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
        
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
        if (
    preg_match("/png/",$system[1]))
        {
            
    imagepng($dst_img,$filename); 
        } else {
            
    imagejpeg($dst_img,$filename); 
        }
        
    imagedestroy($dst_img); 
        
    imagedestroy($src_img); 

    This works fine if the image is located inside the same folder as the page. So if $name is 'apple.jpg' or 'images/apple.jpg' then there is no problem generating the thumbnail.

    However, if the image is located in another folder the thumbnail just comes out black. So I can't use '../images/apple.jpg' and I cant use '/var/myserver/mysite/images/apple.jpg'.

    Is there any way I can get round this?

    I thought it could be something to do with there being more than one '.' in the image location that is killing it but I cant get it to work still.

    Also, it does seem to work if I use the absolute path (/var/myserver/mysite/images/apple.jpg) when saving the image.

  2. #2
    SitePoint Enthusiast Jmz's Avatar
    Join Date
    Jun 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed to fix this

    It was just because of the way the script got the file extension. It got confused if there was more than one '.'

    I used this to get the file extension instead:
    PHP Code:
    $system substr($namestrrpos($name'.') + 1); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In case you're interested...
    PHP Code:
    <?php
    class Image {
        
        private 
    $resource false;
        
        public 
    $mime "";
        
        public 
    $width 0;
        
        public 
    $height 0;
        
        public function 
    __construct($image$raw false$mime null) {
            if (
    $raw) {
                if (
    is_null($mime)) {
                    
    $this->error("Please specify a mime type.");
                }
                
    $this->resource imagecreatefromstring($image);
                
    $this->mime $mime;
            } else {
                
    $this->mime_type($image);
                
    $this->resource $this->load_image($image);
            }
            
    $this->dimensions();
        }
        
        public function 
    draw($w null$h null$raw false) {
            if (
    $this->resource) {
                if (
    is_null($w) && is_null($h)) {
                    
    $out $this->resource;
                } else {
                    
    $w = (is_null($w)) ? $this->width $w;
                    
    $h = (is_null($h)) ? $this->height $h;
                    
    $out imagecreatetruecolor($w$h);
                    
    imagecopyresampled($out$this->resource0000$w$h$this->width$this->height);
                }
                if (!
    $raw) {
                    
    header("Content-Type: ".$this->mime);
                    switch (
    $this->mime) {
                        case 
    "image/jpeg" imagejpeg($out); break;
                        case 
    "image/png" imagepng($out); break;
                        case 
    "image/gif" imagegif($out); break;
                    }
                } else {
                    return 
    $out;
                }
            }
        }
        
        private function 
    mime_type($image) {
            
    $p explode("."$image);
            
    $ext strtolower(end($p));
            
    $ext = ($ext == "jpg") ? "jpeg" $ext;
            
    $this->mime "image/".$ext;
        }
        
        private function 
    dimensions() {
            if (
    $this->img == "") {
                
    $this->width imagesx($this->resource);
                
    $this->height imagesy($this->resouce);
            } else {
                list(
    $w$h) = getimagesize($this->img);
                
    $this->width $w;
                
    $this->height $h;
            }
        }
        
        private function 
    load_image($image) {
            
    $fp fopen($image"r");
            
    $data "";
            while (!
    feof($fp)) {
                
    $data fread($fp1024);
            }
            
    fclose($fp);
            return 
    $data;
        }
        
        private function 
    error($str) {
            
    trigger_error($strE_USER_ERROR);
        }
    }
    ?>
    And to use...

    PHP Code:
    require_once("Image.php");

    function 
    thumb($image$max) {
        
    $img = new Image($image);
        if (
    $img->width $img->height) {
            
    $p = ($max $img->width);
        } else {
            
    $p = ($max $img->height);
        }
        
    $nw ceil($img->width $p);
        
    $nh ceil($img->height $p);
        return array(
    "res" => $img->draw($nw$nhtrue), "mime" => $img->mime);
    }

    $image thumb("/path/to/image.ext"200);
    $thumb_path "/path/to/saved/thumb.ext";
    switch (
    $image['mime']) {
        case 
    "image/jpeg" imagejpeg($image['res'], $thumb_path); break;
        case 
    "image/png" imagepng($image['res'], $thumb_path); break;
        case 
    "image/gif" imagegif($image['res'], $thumb_path); break;
    }
    imagedestroy($image['res']); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •