Need help with timthumb.php

Discussion in 'Programming' started by General Grant, Feb 18, 2010.

  1. #1
    I have a php file that works like timthumb.php, which takes images from another site and creates a thumbnail with it. The problem is, if the image I'm trying to retrieve doesn't exist, I get a broken image. This file is used in a software I have on a site. My question is, does someone have a solution to the code below, that would allow me to show a default image if one isn't available? I would be forever indebted for you if you can help!

    <?php
    /************************************************************
      Download Program ThumbNail Function
    ************************************************************/
       $extension = end(explode(".", $_GET['filename'])); 
     
       if($Size) {$filename=$filename."&Size=".$Size;}
       // Content type
       
        if ($extension == "gif") {
        header("Content-type: image/gif");
    	} elseif ($extension == "png") {
        header("Content-type: image/png");
    	} elseif ($extension == "jpg" || $extension == "jpeg") {
        header("Content-type: image/jpeg");
    	}
       
       
       // Get new dimensions
       list($width, $height) = getimagesize($filename);
       $ratio=$w/$width;
       $new_height = $height * $ratio;
       
       // Resample
       $image_p = imagecreatetruecolor($w, $new_height);
    
        if ($extension == "gif") {
        $image = imagecreatefromgif($filename);
    	} elseif ($extension == "png") {
        $image = imagecreatefrompng($filename);
    	} elseif ($extension == "jpg" || $extension == "jpeg") {
        $image = imagecreatefromjpeg($filename);
    	}
       
       imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $new_height, $width, $height);
       
       // Output
       imagejpeg($image_p, null, 100);
    ?>
    PHP:
     
    General Grant, Feb 18, 2010 IP
  2. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use something like this.

    
    if (file_exists ($filename) == FALSE){
    	$filename = "default.gif";
    }
    
    PHP:
     
    NeoCambell, Feb 21, 2010 IP