Jump to content

Recommended Posts

What is the best way to create a thumbnail from JPG/GIF/PNG images that are uploaded and save them as a separate file. Here is what I would like to do.

 

User uploads a JPG/GIF/PNG image.

A thumbnail is made of that file.

The original and thumbnail are both saved to a directory.

 

I would like to specify that the thumbnail should be no more than 100px wide and have the height automatically adjust to the right proportion

 

I.E.

original image is 200 X 200

thumbnail image will be 100 X 100

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/
Share on other sites

Thanks for the reply but that source was a little more complicated than I needed.  I mean is there a more straight forward way to do it? Ive gone through a ton of web sites and found code like this:

<?php

//Name you want to save your file as
$save = 'myfile.jpg';

$file = 'original.jpg';
echo "Creating file: $save";
$size = 0.45;
header('Content-type: image/jpeg') ;
list($width, $height) = getimagesize($file) ;
$modwidth = $width * $size;
$modheight = $height * $size;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ;
?> 

 

I just cant seem to get the results I want out of it.

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314432
Share on other sites

The code I posted is about as straightforward as it's going to get. You're going to need proportions, check if the image is proportional, if not, find the largest dimension, find out the relation between that and the desired size, and resize your image accordingly.

That's in the middle of the code, which you can cut out if you don't need the multiple image formats or what not.

 

Tutorial doing just that: link

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314544
Share on other sites

How you want it to work depends on if you want the thumbnail created at the time of upload. If that's the case then you'll need two functions working together. One to upload the file, confirm it's uploaded and then the second to create the thumbnail, move it to the required folder and then wrapping everything up with a query that inserts the image name (or possibly the path as well) for the original and the thumb and associate it with the id of the uploader.

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314576
Share on other sites

 

<?php

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

if($filetype == "image/jpeg" || "image/gif" || "image/png"){

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include("mysql_connect.php");

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error: '.mysql_error()); 

mysql_close();



echo "<br />File $fileName uploaded<br />";

}else{

echo "<br />File type ".$filetype." not allowed. Must be JPEG,GIF or PNG.<br />";

} 

}
?>


<fieldset id='submit'><legend>Upload Images</legend>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label id='label'>Select image:</label><input id='input' name="userfile" type="file" id="userfile"><br /> 
<input id='submit' name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</fieldset>

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-314612
Share on other sites

The code dbillings posted inserted the file into a database. You would need to add your own resizing code to that.

Look into move_uploaded_file(), and after you've moved it, take your thumbnail function and use it on that file, generating the thumbnail in the relative directory (thumb/, or whatever you've chosen).

Link to comment
https://forums.phpfreaks.com/topic/63106-php-thumbnail/#findComment-315246
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.