SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creting thumbnail

    Hi

    Cant work out why this code for creating a thumbnail from an image (bogs3.jpg) in my folder 'uploads' isnt working. I have made sure gd2 is in my php5 and is activated. No thumb is created in the thumb folder.

    The page can be seen at: http://www.louandelcats.co.uk/testin...est/thumbs.php

    The function is:

    PHP Code:
    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,$name); 
    } else {
        
    imagejpeg($dst_img,$name); 
    }
    imagedestroy($dst_img); 
    imagedestroy($src_img); 

    The code on the page thumbs.php is:

    PHP Code:
    createthumb('uploads/bogs3.jpg','thumbs/tn_bogs3.jpg'100100); 
    thanks for your thoughts

    cass27

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might have to add some debugging code to the createthumb function to find out where the problem is.

    At a glance the function seems wrong. The $filename argument isn't used anywhere. The imagepng / imagejpeg calls near the end use $name, so the function would probably overwrite the source image with the thumbnail.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for this,

    I shall look into the filename aspect,

    cass27

  4. #4
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    You were right it was the fact it was $name instead of $filename. also I am being lazy by not putting in more error stuff. Trying to make it fundamental to my coding but still learning.

    Thank you for your help

    Nick


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
  •