i was looking for some php code that resizes images effectively creating a thumbnail without resaving and creating a new file. i found this but the problem is that it apparently only works with local urls linking to images files. my images are stored in a mysql database and are retrieved with another php file called "getdata.php?id=x". this php file does work and i can view the original image fine. when i link to http:/mysite.com/pathto/thumbnail.php?url=getdata.php?id=x it gives me this error:

Warning: getimagesize(getdata.php?id=2) [function.getimagesize]: failed to open stream: No such file or directory in /home/public_html/path/thumbnail.php on line 7

this is code for thumbnail.php:

// Get image info from variable
    $url = $_GET['url'];
    $img = getimagesize($url) or die();//line 7

// Checks if URL is image
if ( $img[2] == 1 ) {
    $pic = imagecreatefromgif( $url ) or die();
} elseif ( $img[2] == 2 ) {
    $pic = imagecreatefromjpeg( $url ) or die();
} elseif ( $img[2] ==3 ) {
    $pic = imagecreatefrompng( $url ) or die();
} else {exit();}


// If an image is found and we can determine that it is an image
if ($pic) {
    // Get width and height from the image
    $width = imagesx($pic);
    $height = imagesy($pic);
    $theight = 100;

    // Calculate the new width
    $twidth = ($theight * $width) / $height;

    // Create new image
    $thumb = @imagecreatetruecolor ( $twidth, $theight )
    or die ("Can't create Image!");

    // Resize the image into a thumb
    imagecopyresized($thumb, $pic, 0, 0, 0, 0,
    $twidth, $theight, $width, $height);

    // Change page type to a jpeg
    header ("Content-type: image/jpeg");

    // Create jpeg image from thumbnail
    imagejpeg($thumb,"",75);  

    That's because you can't access a local file with a query string - that's a part of the HTTP protocol, meaning you'd need to make a web request. Making an extra web request seems utterly wasteful, so I would recommend you integrate the retrieval mechanism in getdata.php into this current script.

    Also... any reason why you don't want to cache the newly-resized image in a file? It is much, much more efficient for a server to simply serve up a .jpg file rather than query MySQL, retrieve the full image data, resize it, and then output it to the user.

      give this a try

      // static images width
      $imagewidth = 100;
      // image file
      $image = '/myimages/me.jpg';
      
      function imageResize($width, $height, $staticwidth) {
      	if ($width < $height) {
      		$percent = ($staticwidth / $width);
      	} else {
      		$percent = ($staticwidth / $height);
      	}
      
      $width = round($width * $percent);
      $height = round($height * $percent);
      
      return "width=\"$width\" height=\"$height\"";
      } 
      
      $imagesize = getimagesize($image);
      $attributes = imageResize($imagesize[0],  $imagesize[1], $imagewidth);
      
      echo "<img src=\"$image\" border=\"0\" $attributes>";
      

      by changing the less than to a greater than, it will change it to a static height.

      if ($width > $height) {

        Write a Reply...