Welcome Guest ( Log In | Register )

[Outline] Standard Linear+ > PHP automatic thumbnail maker

MononcQc

post Apr 3 2007, 07:57 PM
Post #1


Better than Chongablonga (like 2 or 3 times)
******

Group: Member
Posts: 8388607
Joined: 13-March 05
From: ' OR 1=1"); //
Member No.: 178


Another tutorial. This one will have minimal security (assuming you uploaded a secure image so it should be safe), and will require you to set the php file and folder containing said file's rights to 0777 (chmod();). To make things short, I'll also only make it work with .jpg files.

I strongly recommend you copy/paste the lines of code into some editor that will put color in them for you, as many of the instructions are given in comments inside the code: it will be easier for you to read that way.

Let's start by the basic function structure:

CODE

<?php
function thumbnail()
{
switch(if the user submitted a name) //if a filename has been sent
{
case "name submitted";
//instructions
break;
default:
//form requesting the image's name
break;
}//end of switch
}//end of function
?>


What we did is create the skeleton of where our script will be. You may be familiar with this, if not, here's what it does in a nutshell: The form will permit the user to type in the name of a file that is in the same directory as the php file (I did it this way so it's shorter, tweak it yourself), and it will send that filename to the function, which will then turn the file into a thumbnail.

Note that this is a function intended to be coupled with the upload function I did earlier, so I will not check security or the existence of the file itself, as when coupled, everything should be fine to begin with. The form and user input are only there for testing purposes.

Here's the form we'll use:
CODE

default:
 echo '<form name="thumbnailer" id="thumbnailer" method="post" target="">
 Enter the filename (only active if in the same directory):
 <br />
 <input type="text" name="filename" id="filename" />
 <br />
 <input type="submit" name="submitthumb" id="submitthumb" value="Thumbnail" />
 </form>';
break;
}
}
?>

So this is fine. Now to make the switch work, we'll use these lines and work on the previous skeleton:

CODE

function thumbnail()
{
switch(@$_POST["submitthumb"]) //if a filename has been sent
{
case "Thumbnail";

we analyse what has been sent in the button (the submit button's name and id are "submitthumb"). If the value of the button is "Thumbnail" (which basically means "if someone pressed the button"), we will go inside this part of the switch instead of default.

Once we are in the 'user has submitted something' part of the switch, we have to request the filename he sent:
CODE
@$imgsource = $_POST["filename"];//request the image name from the form


We will use this source with imagecreatefromjpeg() (to use it with .png and .gif files, use imagecreatefrompng() and imagecreatefromgif()) and then decide its size:

CODE

$source = imagecreatefromjpeg($imgsource) or die("invalid source"); // the picture is the source; if there's an error, nothing's done
 //shaping the image
 $maxwidth = 150;
 $maxheight = 150;
 $dimension = getimagesize($imgsource); //dimensions of original image

sidenote: or die means that if there is an error, the PHP stops being executed there and outputs the error in between the parenthesese.

You have guessed it, if you want different maximal sizes either in height or width, play with $maxwidth and $maxheight. We also requested the dimensions of the original image, as mentionned in the comments.

The next step is about finding out if the image is either taller than wide, or wider than tall. It's going to be done with a simple if, one being the opposite of the other:

CODE

if ($dimension[0] > $dimension[1]) //if width is greater than height
 {
  $width = $maxwidth; //the width becomes greater than height in the thumbnail
  $hratio = $dimension[0]/$maxwidth; //finds out the ratio between the new width and the width of the original picture
  $height = round($dimension[1]/$hratio); //The new height is the result of the original height divided by the ratio
 }
 else
 {
  $height = $maxheight; //the height becomes greater than height in the thumbnail
  $wratio = $dimension[1]/$maxheight;//finds out the ratio between the new height and the height of the original picture
  $width = round($dimension[0]/$wratio);//The new width is the result of the original width divided by the ratio
 }

In simple words, the function just stored the new height and width for the thumbnail in separate variables.

This is going fast, as we're already at the point where we create our empty thumbnail, ready to 'draw' content in it.

CODE

$destination = imagecreatetruecolor($width, $height); // We create an empty thumbnail


Next thing to do is take the dimensions of the original picture and the new empty thumbnail and store them in separate variables that will be used later.

CODE
// The imagesx and imagesy functions return the width and height of a given picture
 $width_source = imagesx($source);
 $height_source = imagesy($source);
 $width_destination = imagesx($destination);
 $height_destination = imagesy($destination);


Ok, almost done: we now store the complete thumbnail inside the code:

CODE

//The thumbnail is created
 imagecopyresampled($destination, $source, 0, 0, 0, 0, $width_destination, $height_destination, $width_source, $height_source) or die("failure of image creation");


And now, we save it!

CODE

//The thumbnail is saved under the filename "thumb_imagesource"
 imagejpeg($destination, 'thumb_'.$imgsource) or die("the thumbnail couldn't be saved");


One little last step, we output it so you can see if it has worked or not (it did for me, so it should for you if you did things right and your server permits all the functions you need)

CODE

echo '<img src="thumb_'.$imgsource.'" title="thumbnailed image" alt="uploaded image" />
 <br />
 The image has been transformed successfully
 <br />
 <a href="">Thumbnail another picture</a>';  


----------

What the final file should look like:
CODE

<?php
function thumbnail()
{
switch(@$_POST["submitthumb"]) //if a filename has been sent
{
case "Thumbnail";
 @$imgsource = $_POST["filename"];//request the image name from the form
 $source = imagecreatefromjpeg($imgsource) or die("invalid source"); // the picture is the source; if there's an error, nothing's done
 //shaping the image
 $maxwidth = 150;
 $maxheight = 150;
 $dimension = getimagesize($imgsource); //dimensions of original image
 //echo $dimension[0];  //used for debug purposes
 //echo $dimension[1];  //used for debug purposes
 
 if ($dimension[0] > $dimension[1]) //if width is greater than height
 {
  $width = $maxwidth; //the width becomes greater than height in the thumbnail
  $hratio = $dimension[0]/$maxwidth; //finds out the ratio between the new width and the width of the original picture
  $height = round($dimension[1]/$hratio); //The new height is the result of the original height divided by the ratio
 }
 else
 {
  $height = $maxheight; //the height becomes greater than height in the thumbnail
  $wratio = $dimension[1]/$maxheight;//finds out the ratio between the new height and the height of the original picture
  $width = round($dimension[0]/$wratio);//The new width is the result of the original width divided by the ratio
 }
 
 $destination = imagecreatetruecolor($width, $height); // We create an empty thumbnail
 
 // The imagesx and imagesy functions return the width and height of a given picture
 $width_source = imagesx($source);
 $height_source = imagesy($source);
 $width_destination = imagesx($destination);
 $height_destination = imagesy($destination);
 
 //The thumbnail is created
 imagecopyresampled($destination, $source, 0, 0, 0, 0, $width_destination, $height_destination, $width_source, $height_source) or die("failure of image creation");
 
 //The thumbnail is saved under the filename "thumb_imagesource"
 imagejpeg($destination, 'thumb_'.$imgsource) or die("the thumbnail couldn't be saved");
 echo '<img src="thumb_'.$imgsource.'" title="thumbnailed image" alt="uploaded image" />
 <br />
 The image has been transformed successfully
 <br />
 <a href="">Thumbnail another picture</a>';  
break;

default:
 echo '<form name="thumbnailer" id="thumbnailer" method="post" target="">
 Enter the filename (only active if in the same directory):
 <br />
 <input type="text" name="filename" id="filename" />
 <br />
 <input type="submit" name="submitthumb" id="submitthumb" value="Thumbnail" />
 </form>';
break;
}
}
?>
<?php
thumbnail();//to run the thumbnail maker
?>


I tried it with this beautiful photoshop of Vangelis riding his piano in front of the Eiffel tower:
user posted image
and obtained:
user posted image

This post has been edited by Dutch Gecko: Apr 3 2007, 08:36 PM

--------------------
 
 
Add ReplyStart TopicStart Poll

Replies

ambons

post Aug 23 2010, 12:02 PM
Post #2


Cesspool Filthling


Group: Members
Posts: 1
Joined: 13-August 10
From: usa
Member No.: 104200


Lemons. a lady-in-waiting of the most powerfully alkalinizing foods, are in the blood to tropical Asia, where cultivation dates chasing at least 2,500 years. In the twelfth century the Arabs brought lemons to Spain and Africa. It was Christopher Columbus, who brought the seeds of lemons with him from the Canary Islands on his subscribe to voyage. In the Modish Men, lemons were introduced not later than the Spanish adventurers in Haiti, then known as Hispaniola. In the Cooperative States, Florida was the in the launch stock lemon-producing limit, and this glory led in put of lemons until a grave cheer in 1895 killed the lemon groves. They were not in any nature replanted. In the contribution circumstances, thither 95 percent of the lemons in necessitate normal to in the Intersection States and Canada are produced in southern California. The other 5 percent are grown in Italy. Italy and California together cook up verging on the ‚lite's bask in song in of lemons
 

Posts in this topic

MononcQc   PHP automatic thumbnail maker   Apr 3 2007, 07:57 PM
Dutch Gecko   Done.   Apr 3 2007, 08:35 PM
snunseplalops   spam deleted   Dec 25 2008, 06:19 PM
assognonY   deleted   Jan 22 2009, 10:41 PM
Idowlydob   spam deleted   Feb 10 2009, 01:57 PM
megga   The. concept exists conceptually in other nations,...   May 16 2010, 03:15 PM
patrick076   nice work man   May 26 2010, 02:49 AM
boomb   homemoney. to purchasehow It would be fully to ass...   Jun 1 2010, 03:21 PM
irisbom   Many.Multitudinous things accounted in behalf of s...   Jun 4 2010, 08:05 PM
Pluhfelve   About: For what DDoS: 99% go on this link. In i...   Jun 28 2010, 08:34 AM
arolaWhomohow   Lustful stood during the course of Cindy, stroking...   Jul 7 2010, 04:57 PM
maleenhancements   Reads my reviews about male enhancement pills befo...   Jul 27 2010, 11:10 PM
maleenhancements   Reads my reviews about penis enlargement befor you...   Aug 5 2010, 02:20 PM
Add ReplyOptionsStart Topic

 

 

 

Lo-Fi Version Time is now: 9th September 2010 - 05:03 AM