Jump to content

Recommended Posts

I have a great image upload script, thanks to some members here. The image gets uploaded to the server, and the image path goes to mysql. The problem I am having is that i hate smashing a 1200x1600 pic into a box 125x125 because the image size is still like 900KB. So on upload, I would like to create a thumb of the pic and have both the full size and the thumb uploaded to the server. When the thumbnail is created, it will have a smaller file size, correct? Would my entire upload script have to be modified or is this something fairly easy to accomplish? any input would be appreciated. thanks.

Link to comment
https://forums.phpfreaks.com/topic/164027-solved-create-thumbs-on-pic-upload/
Share on other sites

resizing the image and saving it under another name shouldn't be a problem, and you should be-able to plug it into your existing uploader

 

you could create a function, heres some basic image resize code

<?php
// The file
$filename = 'test.jpg';
$NewFile  = 'test2.jpg';

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, $NewFile, 100);

?>

Here is my upload script now. What I am worried about is having the same stamp name on both the thumbnail and the full size pic. I am still pretty new to this, and integrating the thumbnail into this script seems like a nightmare to me.

<?php
session_start();
include "connection.php";

$item_name = mysql_real_escape_string($_POST['item_name']);
$description = mysql_real_escape_string($_POST['description']);
$in_return = mysql_real_escape_string($_POST['in_return']);
$category = mysql_real_escape_string($_POST['listmenu']);

define ("MAX_SIZE","1500");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
	return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;
if(isset($_POST['submit']))
{
$image=$_FILES['image']['name'];
if($image) 
{

	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png"))
	{
		show error here.	
	}
	else
	{
		$size=filesize($_FILES['image']['tmp_name']);
		if ($size > MAX_SIZE*1024)
		{
			show error here			}

		$image_name=time().'.'.$extension;
            $newname="userimages/$category/".$image_name;

		$copied = copy($_FILES['image']['tmp_name'], $newname);

		if (!$copied)
		{
			show error here
		}

	}
}
}

// if everything is good, post new item for the user
$mysqlcategory = $category;
$imgpath = $newname;
$findit = $_SESSION['id'];
$result=mysql_query("SELECT id FROM members WHERE username = '$findit'");
$row=mysql_fetch_assoc($result);
$user_id = $row['id'];
$sql = "INSERT INTO member_pics(item_name, description, in_return, imgpath, category, user_id)VALUES('$item_name','$description','$in_return', '$imgpath', '$mysqlcategory', '$user_id')";
mysql_query($sql) or die(mysql_error());
?>

*untested* but should be okay

 

change

$image_name=time().'.'.$extension;
$newname="userimages/$category/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname);

to

$tname = time();
$image_name=$tname.'.'.$extension;
$newname="userimages/$category/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);

$image_thumb=$tname.'-thumb.'.$extension;
$newthumbname="userimages/$category/".$image_thumb;

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($newname);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($newname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, $newthumbname, 100);
imagedestroy($image);
imagedestroy($image_p);

 

I have been trying to have the thumbs upload as follows.

if the picture is taller than its width, then set max height at 150px

and, if the picture is wider than its height, well, it works good as is for accomplishing that.

 

Just having problems width setting a max height at 150 but still keeping the correct pic ratio.

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.