SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Resizer and thumbnailer

    I found this code and it works awesome.
    The problem is, if the pic is missing it comes up as no picture.
    I would like it to print a "No Image" picture on the page.
    Is there a way to do this?

    Here is the code:
    Code:
    <?php 
    // Image thumbnail0r (with an "0" :]) 
    // mike[@]filespanker.com 
    // 
    // PARAMETERS: 
    // 
    // img: 
    // The image file as relative to THIS script. 
    // h: 
    // The thumbnail's height. Default: 30 
    // w: 
    // The thumbnail's width. Default: 30 
    // mode: 
    // 1 = stretch: The image is resized to height and width 
    // 0[default] = proportioned: The image is shrunken, but keeps proportions 
    // type: 
    // [optional] 
    // jpg = JPEG 
    // gif = GIF 
    // png = PNG 
    // If this is not set, it is determined by its file extension. 
    // 
    // This script's functions rely completely on your gd lib version. 
    // 
    // So, if I recall correctly: 
    // gd v1.5 or lower : GIF 
    // gd v1.6 or higher: PNG 
    // gd v1.8 or higher: PNG and JPEG 
    // 
    // So, all three image types should never work on the same gd lib :[ 
    // You can thank Unisys for that. 
    // 
    // Before mailing me, please actually look at the code. 
    //Theres not much I could have really screwed up, and its probably an 
    // issue with your gd library. Try up/downgrading it. 
    // Configuration: 
    
    //// CODE 
    if (!isset($w)) 
    { 
    $w = 100; 
    } 
    if (!isset($h)) 
    { 
    $h = 100; 
    } 
    
    SetType($mode, 'integer'); 
    SetType($w, 'integer'); 
    SetType($h, 'integer'); 
    SetType($img, 'string' ); 
    function percent($p, $w) 
    { 
    return (real)(100 * ($p / $w)); 
    } 
    function unpercent($percent, $whole) 
    { 
    return (real)(($percent * $whole) / 100); 
    } 
    // Initialization 
    // Make sure the file exists... 
    if (!file_exists($img)) 
    { 
    echo "ERROR: $img not found"; 
    exit(); 
    } 
    // If the user defined a type to use. 
    if (!isset($type)) 
    { 
    $ext = explode('.', $img); 
    $ext = $ext[count($ext)-1]; 
    switch(strtolower($ext)) 
    { 
    case 'jpeg' : 
    $type = 'jpg'; 
    break; 
    default : 
    $type = $ext; 
    break; 
    } 
    } 
    // Create the image... 
    switch (strtolower($type)) 
    { 
    case 'jpg': 
    $tmp = imagecreatefromjpeg($img); 
    break; 
    case 'gif': 
    $tmp = @imagecreatefromgif($img); 
    break; 
    
    case 'png': 
    $tmp = @imagecreatefrompng($img); 
    break; 
    
    default: 
    echo 'Error: Unrecognized image format.'; 
    exit(); 
    break; 
    } 
    if ($tmp) 
    { 
    // Resize it 
    
    $ow = imagesx ($tmp); // Original image width 
    $oh = imagesy ($tmp); // Original image height 
    if ($mode) 
    { 
    // Just smash it up to fit the dimensions 
    $nw = $w; 
    $nh = $h; 
    } 
    else 
    { 
    // Make it proportional. 
    if ($ow > $oh) 
    { 
    $nw = $w; 
    $nh = unpercent(percent($nw, $ow), $oh); 
    } 
    else if ($oh > $ow) 
    { 
    $nh = $h; 
    $nw = unpercent(percent($nh, $oh), $ow); 
    } 
    else 
    { 
    $nh = $h; 
    $oh = $w; 
    } 
    } 
    $out = imagecreate($nw, $nh); 
    imagecopyresized($out, $tmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh); 
    imagedestroy($tmp); 
    } 
    else 
    { 
    echo 'Could not create image resource.'; 
    exit; 
    } 
    
    if ($out) 
    { 
    switch (strtolower($type)) 
    { 
    case 'jpg': 
    header('Content-type: image/jpeg'); 
    imagejpeg($out); 
    break; 
    case 'gif': 
    header('Content-type: image/gif'); 
    imagegif($out); 
    break; 
    case 'png': 
    header('Content-type: image/png'); 
    imagepng($out); 
    break; 
    } 
    imagedestroy($out); 
    } 
    else 
    { 
    echo 'ERROR: Could not create resized image.'; 
    } 
    ?>

    Here is the section with the file not found:
    Code:
    // Make sure the file exists... 
    if (!file_exists($img)) 
    { 
    echo "ERROR: $img not found"; 
    exit(); 
    }
    I tried everything I can think of to make it work.
    any help,
    THanks,
    Kevin
    I PHP!

  2. #2
    SitePoint Enthusiast rev909's Avatar
    Join Date
    Mar 2002
    Location
    Romania
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    {
    echo 
    "ERROR: $img not found";
    exit();

    Instead of this, open your image which contains the NO IMAGE graphic and output it.

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup, just

    {
    header("Content-type image/jpeg");
    fread('path/missing_image.jpg');
    }

    will suffice.

    [blatant plug] if you fancy going a step or two further with your thumbnails - eg bevel, drop shadow, motion blur, overlay etc - take a look at this class[/blatant plug]
    teckis - that's news to me.

  4. #4
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that did it!
    Thank you!
    Kevin
    I PHP!

  5. #5
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this works perfectly:

    // Make sure the file exists...
    if (!file_exists($img))
    {
    header("Content-type: image/jpeg");
    include("http://www.kevinslair.com/images/Tarot-Cards.jpg");
    exit();
    }

    Thanks all!
    Kevin
    Last edited by Kevinslair; Jul 13, 2003 at 11:52. Reason: error
    I PHP!

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there anyway to quickly format that code snippet into readable code? could someone repost it if possible?

    thanks


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
  •