Jump to content

problem with imagecopyresized function


albridge

Recommended Posts

i am trying to build a membership site where members can select search criteria like male or female and age to view profiles of other members on the system. on the result page i want to dynamically generate thumbnail like immages (smaller images) from an uploaded image of each member. i have tried to do this with php gd function 'imagecopyresized' and while it does successfully resize the image, it appears to only do so for the first or last image cos all the outputted images on the search result page are the same. however, if only one member on the search result has uploaded a profile image then only that image is uploaded in the right position and correctly. i have written the imageresize script in a seperate file and inlclude it in a while loop as the query returns matching profiles. i do this so i can pass the unique id of each member to the include file with which it is supposed to get, resize and output the appropriate image of each member. been on this for two days now. would really appreciate some help. please take it slow with me cos i am still new to php. the code is shown below.

<?php$query="select photo from testmember where id='".$rowdd['id']."'";$result=mysql_query($query);$row=mysql_fetch_array($result);// Crop dimensions.$width = 100;$height = 100;//if(!empty($row['photo'])){$imd=$row['photo']; // Set the path to the image to resize$input_image = $imd;// Get the size of the original image into an array$size = getimagesize( $input_image );// Prepare canvas$canvas = imagecreatetruecolor( $width, $height );// Create a new image in the memory from the file $cropped = imagecreatefromjpeg( $input_image );// Prepare image  crop - center the crop on the image$newwidth = $size[0] / 2;$newheight = $size[1] / 2;$cropLeft = $size[0]/4; //( $newwidth/2 ) - ( $width/2 );$cropHeight = ( $newheight/2 ) - ( $height/2 );// Generate the cropped imageimagecopyresized( $canvas, $cropped, 0,0, $cropLeft, $cropHeight, ($size[0]/2), ($size[1]/2), $newwidth, $newheight );// Save the cropped image as cropped.jpgimagejpeg( $canvas, "cropped.jpg" );//$neewname="images/cropped.jpg";//copy("cropped.jpg","images/cropped.jpg");// Clear the memory of the tempory imagesecho "<img src='cropped.jpg'>";imagedestroy( $cropped );imagedestroy( $canvas );//}$cropped="";?>

i try to make use of the output of the code above like this

		<td width="132" height="148" style="vertical-align:top "><?php if(!empty($rowdd['photo'])){ include('cropimage.php');} ?></td>

the funny thing is that if i refresh the result page many times enough (like 20 or 30 time) the page sometimes displays the appropriate images in beside the right member. i dont get itplease someone kindly hook me up. if you have a better or easier way that i can easily understand cos like i said i am still new to this and most of this php stuff still sound like martian language to me so the easier to understand the better. thank again.

Link to comment
Share on other sites

I don't know how to use that function, but you should ask yourself if it's vital to create a thumbnail. Why not just take the user's current picture and shrink it down to size with some CSS?Add this to your CSS code:

.resize_image{	width: 100px;	height: 100px;}

Then, for each picture that needs it, you can do this:

<img class="resize_image" src="image.jpg" />

And since you really don't know if someone will have a profile picture or not, you need to define a default one so that someone not having one won't break up your layout.Probably something like...

<?phpif (empty($rowdd['photo']){echo '<img src="my_default_pic.jpg" />';}else{echo '<img class="resize_image" src="image.jpg" />';}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...