resize image before display

Discussion in 'PHP' started by dougvcd, Jun 13, 2007.

  1. #1
    this is the code i use to fetch info from database
    is there a way to limit the size of the photo to a certain size to display

    //Outputs the image and other data
    Echo "<img src=http://www.caravan-holiday-exchange.com/images/".$info['photo'] ."> <br>";
    Echo "<b>Park Name:</b> ".$info['parkname'] . " <br>";
    Echo "<b>Park Location:</b> ".$info['parklocation'] . " <br>";
    Echo "<b>Details:</b> ".$info['caravandetails'] . " <hr>";
    PHP:
    cheers
    Doug
     
    dougvcd, Jun 13, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    use the gd2 library. it'd be better if you saved the images resized when uploaded but if you want to do it when the page loads then use something like this

    
    <?php
    $imagepath = $_GET['image'];
    $myHeight = 100;
    $myWidth = 100;
    
    if(file_exists($imagepath)){
    	$size = getimagesize($imagepath);
    	$source_x = $size[0];
    	$source_y = $size[1];
    	$source_id = imagecreatefromjpeg($imagepath);
    	$target_id = imagecreatetruecolor($myWidth, $myHeight);
    	$target_pic = imagecopyresampled($target_id, $source_id, 0, 0, 0, 0, $myWidth, $myHeight, $source_x, $source_y);
    	imagejpeg($target_id);
    }
    
    //lets assume that this file is named "thumbnail.php"
    //in another file, <img src="thumbnail.php?image=myimage.jpg" alt="thumbnail" />
    ?>
    
    PHP:
     
    ansi, Jun 13, 2007 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks
    Doug
     
    dougvcd, Jun 13, 2007 IP