dennismonsewicz Posted April 7, 2008 Share Posted April 7, 2008 I am trying to create a thumbnail of an image on the fly, but its not working. Here is my code: // load image and get image size $img = imagecreatefromjpeg( "{$url}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); header("Content-Type: image/jpeg"); // create a new temporary image $tmp_img = imagejpeg( $new_width, $new_height ); This is my code to display the image: <img src="<?php echo $tmp_img; ?>" border="0" /> Any help? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 bump Quote Link to comment Share on other sites More sharing options...
craygo Posted April 7, 2008 Share Posted April 7, 2008 Use this function and put it in it's own file. say imageresize.php <?php /********************************** * Will resize an image to a * * max width or height and keep * * aspect ratio Name this file * * anything you like. * * I will use imageresize.php * **********************************/ header('Content-type: image/jpeg'); function resampleimage($maxsize, $sourcefile, $imgcomp=0) { $g_imgcomp=100-$imgcomp; if(file_exists($sourcefile)) { $g_is=getimagesize($sourcefile); if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){ $new_width=$g_is[0]; $new_height=$g_is[1]; } else { $w_adjust = ($maxsize / $g_is[0]); $h_adjust = ($maxsize / $g_is[1]); if($w_adjust <= $h_adjust) { $new_width=($g_is[0]*$w_adjust); $new_height=($g_is[1]*$w_adjust); } else { $new_width=($g_is[0]*$h_adjust); $new_height=($g_is[1]*$h_adjust); } } //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . ) $image_type = strrchr($sourcefile, "."); //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($image_type) { case '.jpg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.png': $img_src = imagecreatefrompng($sourcefile); break; case '.gif': $img_src = imagecreatefromgif($sourcefile); break; default: echo("Error Invalid Image Type"); die; break; } $img_dst=imagecreatetruecolor($new_width,$new_height); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]); imagejpeg($img_dst); imagedestroy($img_dst); imagedestroy($img_src); return true; } else return false; } // now call the image from any other page with <img src="imageresize.php?maxsize=xxx&source=path/to/image/file" border=0 /> resampleimage($_GET['maxsize'], $_GET['source']); ?> now call it like <img src="imageresize.php?maxsize=xxx&source=<? echo $tmp_img; ?>" border="0" /> Ray Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 do I insert my own maxsize? Quote Link to comment Share on other sites More sharing options...
craygo Posted April 7, 2008 Share Posted April 7, 2008 yes. This script will keep the original aspect ratio so picture won't be distorted. Ray Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 ok sweet, next question: how does this file know where to grab the sourcefile from? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 ok disregard what I just asked... I tried your script and got nothing: <?php $imagename = $result['name']; $imagesize = $result['size']; $kbsize = number_format($imagesize / 1024); $url = $result['url']; $uploadedby = $result['username']; $cats = $result['categories']; $numberofdownloads = $result['number_of_downloads']; ?> <div class="maincontent"> <div class="maincontentheader"> <h2><?php echo ucwords($username); ?> here is <?php echo $imagename; ?>'s information!</h2> </div> <table width="690" cellpadding="4" cellspacing="0" border="0" align="center"> <tr> <td bgcolor="#dcdcdc" width="325" align="center" style="border: 1px solid #666666;"><img src="imageresize.php?maxsize=150&source=<? echo $imagename; ?>" border="0" /></td> <td valign="top"><p><b>Image Name:</b> <?php echo $imagename; ?></p> <p><b>Image Size:</b> <?php echo $kbsize; ?> KB</p> <p><b>Uploaded By:</b> <?php echo ucwords($uploadedby) ?></p> <p><b>Categories:</b> <?php echo $cats ?></p> <p><b>Number of Downloads:</b> <?php echo $numberofdownloads ?></p> </td> </tr> </table> <div class="maincontentfooter"> </div> </div> <?php include "includes/footer.php"; ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 7, 2008 Share Posted April 7, 2008 go toimageresize.php?maxsize=150&source=VALIDSOURCE HERE and see if it is erroring via viewing its source code. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 I went to it in the URL, and it works, but it isn't working when i call it in the src <img src="imageuploads/imageresize.php?maxsize=500&source=<? echo $imagename; ?>" border="0" /> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted April 7, 2008 Share Posted April 7, 2008 check your source code to make sure your image is a valid image location Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted April 7, 2008 Author Share Posted April 7, 2008 my server is setup to use <?php not <? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.