Jump to content

convert image into thumbnail before displaying them.


php_begins

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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);
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.