php_begins Posted October 4, 2011 Share Posted October 4, 2011 I have a form where you can enter a image url and it's title. It calls display.php which displays the image as well as the title. But I want the image to be displayed as a thumbnail of size 150X100. Do i have to store it in directory before I convert it and display? If so, can somebody tell me how to convert the image into a thumbnail? Here is my code: <table align="center" bgcolor="silver"> <form action="display.php" method ="post"> <tr> <td align="center"><b><font color="#214754" size="3">Reg</font></b></td> </tr> <tr> <td> Title: </td> <td><input type="text" size ="40" name="registry_title1" /></td> </tr> <tr> <td> Image URL: </td> <td><input type="text" size ="40" name="registry_img1" /></td> </tr> <tr> <td> <input type="submit" name="submit" value="submit" /> </td> </tr> </form> </table> </div> here is the code for display.php <? /* Get Registry details */ $registry_img1=$_POST['registry_img1']; $registry_title1=stripslashes($_POST['registry_title1']); ?> <html> <body> <table width='600' cellspacing='0' cellpadding='0' border='0' bgcolor='#ffffff' align='center'> <tr> <td style='padding-bottom:8px';><font style='font-size:18px;color:rgb(0, 0, 0);text-decoration:none'><a style='font-size:18px;color:rgb(0, 0, 0);text-decoration:none' href='#'><? echo ucfirst($url); ?> Registry</font></a></td> </tr> <tr> <td style='padding-bottom:10px;'> <a style='color:rgb(206, 0, 0);font-size:14px;font-weight:bold;line-height:18px;text-decoration:none' href='<? echo $registry_url1; ?>' target='_blank'><img style='padding-right:10px' border='0' align='left' alt='' src='<? echo $registry_img1; ?>'><? echo $registry_title1;?> </a> </td> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 4, 2011 Share Posted October 4, 2011 What you should do will be driven by the purpose. When the user supplies the URL do you *want* to store that image locally (i.e.on the server)? If so, then you should copy the image to the server then make a thumbnail copy. If you only intent to store the URL and always get the source of the image from that URL, then it would be inefficient to create a thumbnail. Instead, you should use getimagesize() to get the height/width of the image and then determine the appropriate dimensions for the thumb and use those in the image tag. Quote Link to comment Share on other sites More sharing options...
php_begins Posted October 4, 2011 Author Share Posted October 4, 2011 thanks for the suggestions. I would like to go with the first option. I am having trouble copying them to my server and then applying some resize function to create the thumbnail. The tutorials I found in the net are somewhat complicated and not straightforward. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 4, 2011 Share Posted October 4, 2011 Well, pick the one that seems the least complicated, implement it step-by-step and continue until you run into problems. Then post here. However, I know I can find plenty of ready made image resizing/thumb creating functions with simple google searching. So, I'm not sure what your difficulty is. Although, those are usually based upon you already having the image or an uploaded image. So, do some searching on how to copy an image using a URL - then work on resizing it. Quote Link to comment Share on other sites More sharing options...
floridaflatlander Posted October 5, 2011 Share Posted October 5, 2011 I used the book "php soluitons" to learn how to do it, There is a PDF version on line, I have both (they're the older versions and as a note you can get the older book at amazon for a deal. The newer one deals with photo uploads and such but works more with objects). I move my photos from the servers temp folder and create both a large photo for display and a thumb nail at the same time. Reducing the quailty(pixal size) for the internet, I use 50 with no problem. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted October 5, 2011 Share Posted October 5, 2011 I have a form where you can enter a image url and it's title. It calls display.php which displays the image as well as the title. But I want the image to be displayed as a thumbnail of size 150X100. Do i have to store it in directory before I convert it and display? If so, can somebody tell me how to convert the image into a thumbnail? Here is my code: <table align="center" bgcolor="silver"> <form action="display.php" method ="post"> <tr> <td align="center"><b><font color="#214754" size="3">Reg</font></b></td> </tr> <tr> <td> Title: </td> <td><input type="text" size ="40" name="registry_title1" /></td> </tr> <tr> <td> Image URL: </td> <td><input type="text" size ="40" name="registry_img1" /></td> </tr> <tr> <td> <input type="submit" name="submit" value="submit" /> </td> </tr> </form> </table> </div> here is the code for display.php <? /* Get Registry details */ $registry_img1=$_POST['registry_img1']; $registry_title1=stripslashes($_POST['registry_title1']); ?> <html> <body> <table width='600' cellspacing='0' cellpadding='0' border='0' bgcolor='#ffffff' align='center'> <tr> <td style='padding-bottom:8px';><font style='font-size:18px;color:rgb(0, 0, 0);text-decoration:none'><a style='font-size:18px;color:rgb(0, 0, 0);text-decoration:none' href='#'><? echo ucfirst($url); ?> Registry</font></a></td> </tr> <tr> <td style='padding-bottom:10px;'> <a style='color:rgb(206, 0, 0);font-size:14px;font-weight:bold;line-height:18px;text-decoration:none' href='<? echo $registry_url1; ?>' target='_blank'><img style='padding-right:10px' border='0' align='left' alt='' src='<? echo $registry_img1; ?>'><? echo $registry_title1;?> </a> </td> </table> </body> </html> I have a function that I use to make thumbnails that uses PHP's GD functions.. however make sure that GD is compiled with your PHP config Quote Link to comment Share on other sites More sharing options...
php_begins Posted October 5, 2011 Author Share Posted October 5, 2011 ok I shall try using PHP GD. Thanks for your responses.. Quote Link to comment Share on other sites More sharing options...
php_begins Posted October 5, 2011 Author Share Posted October 5, 2011 ok I finished copying the url image to my directory and applied the image_resize function to create the thumbnail. First of all,it does not work according to the dimensions i give.For example, 80X80 gives me 80X66 thumbnail. Secondly, the image gets distorted(stretched at times). Is it possible to retain the original quality of image like how facebook does in its thumbnails. Here is my resize function: <? function img_resize($name,$filename,$new_w,$new_h) { $system=explode(".",$name); if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);} if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);} if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);} $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (preg_match("/png/",$system[1])) { imagepng($dst_img,$filename); } elseif (preg_match("/gif/",$system[1])) { imagegif($dst_img,$filename); } else { imagejpeg($dst_img,$filename); } } ?> I call it like this: img_resize('images/'.$thumbnail_review_1,'images/thumb_'.$thumbnail_review_1,80,80); Quote Link to comment Share on other sites More sharing options...
djlee Posted October 5, 2011 Share Posted October 5, 2011 The stretching is because your maths is wrong. im guessing you want it so if the width is more than the height. then the new width is applied, and the same scaling ratio from old to new width is applied to the new height so /** * $w = max width the thumbnail can be * $h = max height the thumb should be * $old_w, $old_h = source image dimensions * $new_w, $new_h = the dimensions you should resize the source image to, to create a thumbnail */ if($old_w > $old_h) { $new_w = $w; $scale_factor = $w/$old_w; $new_h = $old_h * $scale_factor; } else { ///do the opposite } Quote Link to comment Share on other sites More sharing options...
php_begins Posted October 5, 2011 Author Share Posted October 5, 2011 The quality of the image is still pretty poor. There is still distortion. Quote Link to comment Share on other sites More sharing options...
php_begins Posted October 5, 2011 Author Share Posted October 5, 2011 I have used the below function to crop and create the thumbnail, so that it doesn't look distorted. function CroppedThumbnail($source_image_name,$destination_image_name,$specified_width,$specified_height) { $system=explode(".",$source_image_name); list($old_width, $old_height) = getimagesize($source_image_name); $ratio_orig = $old_width/$old_height; $fp = fopen($source_image_name, 'rb'); $buf = ''; while(!feof($fp)) { $buf .= fgets($fp, 4096); } fclose($fp); $myImage=imagecreatefromstring($buf); if($old_width < $specified_width && $old_height < $specified_height) { $dst_img=ImageCreateTrueColor($old_width,$old_height); imagecopyresampled($dst_img,$myImage,0,0,0,0,$old_width,$old_height,$old_width,$old_height); if (preg_match("/png/",$system[1])) { imagepng($dst_img,$destination_image_name); } else { imagejpeg($dst_img,$destination_image_name); } imagedestroy($dst_img); imagedestroy($myImage); } else { //In this section we will actually resize and crop it. if($specified_width/$specified_height > $ratio_orig) { $new_height = $specified_width/$ratio_orig; $new_width = $specified_width; } else { $new_width = $specified_height*$ratio_orig; $new_height = $specified_height; } $x_mid = $new_width/2; //horizontal middle $y_mid = $new_height/2; //vertical middle $bgcolor = imagecolorallocate($process, 255, 255, 255); imagefill($process, 0, 0, $bgcolor); $process = imagecreatetruecolor(round($new_width), round($new_height)); imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); $thumb = imagecreatetruecolor($specified_width, $specified_height); $bgcolor = imagecolorallocate($thumb, 255, 255, 255); imagefill($thumb, 0, 0, $bgcolor); imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($specified_width/2)), ($y_mid-($specified_height/2)), $specified_width, $specified_height, $specified_width, $specified_height); if(preg_match("/png/",$system[1])) { imagepng($thumb,$destination_image_name); } else { imagejpeg($thumb,$destination_image_name); } imagedestroy($process); imagedestroy($myImage); } } 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.