Thread: Image resize

  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Rep Power

    Image resize

    hey guy's i have this page that fetch's imagis in my fine the only thing is i would like to resize the image?is there a way to incorparte it in my script?

    PHP Code:
    // Connect to database 
    $errmsg "";
    if (! @
    mysql_connect("localhost","1_techker","1")) {
    $errmsg "Cannot connect to database";
    // Find out about images to display 
    $pid mysql_escape_string($_GET['pid']);
    if (
    $_GET['pid']) { 
    $whereclause "where pid = \"$pid"
    $gotten = @mysql_query("select * from pix  order by pid desc "); 
       while (
    $row = @mysql_fetch_assoc($gotten)) { 
    $imcolumn .= "<tr><td><img src=?pid=$row[pid] width= '144' ><br>"
    $imcolumn .= htmlspecialchars($row[title])."</td></tr>"
       if (! 
    $nim$imcolumn "<tr><td>No matching images</td></tr>"
    } else { 
    $imcolumn .= "<tr><td>Images will appear here</td></tr>"

    // If this is the image request, send out the image 
    if ($_GET['pid']) { 
    $gotten = @mysql_query("select * from pix where pid = $_GET[pid]"); 
    $row mysql_fetch_assoc($gotten); 
    $bytes $row[imgdata]; 
    header("Content-type: image/jpeg"); 
       exit (); 
    <title>Selection of up to 3 images from a database</title> 
    <body bgcolor=white><h2>&nbsp;</h2> 
    <font color=red><?= $errmsg ?></font> 
    <center><table border=1> 
    <b><?= $imcolumn ?></table></center> 


    Comments on this post

    • Arty Ziff agrees : You need some points!
  2. #2
  3. Null Pointer Exception
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2006
    Rep Power
    First of all you shouldn't store images directly into your database, I don't know where everyone is learning to do that from. It's a very bad idea. Instead you should store urls or references to images that are located on your disk.

    Second of all, if you want to perform resizing operations on your image, I would google for something called "phpthumb" which lets you perform resizing operations.
  4. #3
  5. No Profile Picture
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Apr 2005
    Rep Power
    Look at the php GD functions. Here's a linky to php image function documentation. Most (all?) of these are GD library functions. You'll hear a lot about ImageMagick, and it has a lot going for it, but it's not always installed of shared hosts. GD is virtually ALWAYS installed:

    I found a load of beginner tutorials with Google.

    You really don't need the overhead of a big application / library for simple resizing.

    How about something like this:
    //keep image type
    if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
    $new_img = imagecreatefromjpeg($file_tmp);
    }elseif($file_type == "image/x-png" || $file_type == "image/png"){
    $new_img = imagecreatefrompng($file_tmp);
    }elseif($file_type == "image/gif"){
    $new_img = imagecreatefromgif($file_tmp);

    //list the width and height and keep the height ratio.
    list($width, $height) = getimagesize($file_tmp);

    //calculate the image ratio
    if ($imgratio>1){
    $newwidth = $ThumbWidth;
    $newheight = $ThumbWidth/$imgratio;
    $newheight = $ThumbWidth;
    $newwidth = $ThumbWidth*$imgratio;

    //function for resize image.
    if (function_exists(imagecreatetruecolor)){
    $resized_img = imagecreatetruecolor($newwidth,$newheight);
    die("Error: Please make sure you have GD library ver 2+");

    //the resizing is going on here!

    imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    //finally, save the image
    ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
    ImageDestroy ($resized_img);
    ImageDestroy ($new_img);
    Also as misterdanny says, storing images as blobs in a database is not optimal.

    Comments on this post

    • misterdanny agrees : phpthumb uses the GD Libray, I proposed using phpthumb because it's easier than using GD directly.
    Last edited by Arty Ziff; November 17th, 2008 at 12:30 AM.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2005
    Rep Power
    thx guys im studying now gd so i will see.phpthumb is cool to.thx

IMN logo majestic logo threadwatch logo seochat tools logo