Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Recommended Posts

I have an image uploader which is causing images to stretch when they are being resized, which i do not want.

 

I need the image to be resized 3 times and moved into 3 different folders, but i do not want the image to be scaled up if it is too big.

 

Here is the code i'm working with;

 

<?php

include('include/session.php');
if($session->logged_in){

ini_set("session.gc_maxlifetime","10800");
ini_set("upload_max_size","4194304");
$default = $_POST["default"];
$username = $session->userinfo['username'];

if(isset($_FILES))
{
$fileName = $_FILES["uploadfile"]["name"];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
do{
$randName = md5(rand() * time());
$pass = "$randName.$ext";
$Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass"));
}while( $Check );
$uploadedfile = $_FILES["uploadfile"]["tmp_name"];

list($width,$height)=getimagesize($uploadedfile);

$newwidth1=600;
$newwidth2=180;
$newwidth3=100;
$newheight1=($height/$width)*600;
$newheight2=($height/$width)*180;
$newheight3=($height/$width)*100;

switch($_FILES['uploadfile']['type'])
{
case "image/gif":
	$src = imagecreatefromgif($uploadedfile);//Create from GIF!
	$type = "gif";
break;
case "image/jpg":
case "image/jpeg":
	$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!
	$type = "jpg";
break;
}
#1
//$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG!
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = "upimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp1,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp1,$filename,100);
}
imagedestroy($src);

#2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1);
$filename = "thimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp2,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp2,$filename,100);
}
imagedestroy($tmp1);

#3
$tmp3=imagecreatetruecolor($newwidth3,$newheight3);
imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2);
$filename = "avimg/$pass";// . $_FILES["uploadfile"]["name"];
if($type == "gif")
{
imagegif($tmp3,$filename,100);
}elseif($type == "jpg"){
imagejpeg($tmp3,$filename,100);
}
imagedestroy($tmp2);
imagedestroy($tmp3);

$name = $_FILES["uploadfile"]["name"];
}}

# $query = "INSERT INTO track9_userphotos (pictureurl, username, caption) VALUES('$pass', '$username', '$caption')";
# mysql_query($query) or die(mysql_error());

if ( $default == 1 ) {
mysql_query("UPDATE ".TBL_USERS." SET photo = '$pass' WHERE username = '$username'");
} else { echo ""; }


?>
<h8>Your Uploaded Picture</h8><p><div id='header2'>
<br><br>
<table border="0" width="700" cellspacing="0" cellpadding="0">
<tr>
	<td width="200" valign="top">
	<p align="center"><? echo "<img src='thimg/$pass'>"; ?></p>
	<p></td>
	<td>
	<p align="center">  
<p align="center"> </p>
	</form>
	<p></td>
</tr>
</table>
</div>

Link to comment
https://forums.phpfreaks.com/topic/129149-image-uploader-modification/
Share on other sites

heres what i use

 

<?php

//set max height and width for bgimage
$bg_height = 900;
$bg_width  = 900;
$bg_pixels = 900;

//set max height and width for smimage
$sm_height = 100;
$sm_width  = 100;
$sm_pixels = 100;

//big
$new_bg_height=($height_orig/$width_orig)*$bg_pixels;
$tmp_bg=imagecreatetruecolor($bg_width,$new_bg_height);
//small
$new_sm_height=($height_orig/$width_orig)*$sm_pixels;
$tmp_sm=imagecreatetruecolor($sm_width,$new_sm_height);
// these lines actually do the image resizing, copying from the original
// image into the $tmp image
	imagecopyresampled($tmp_bg,$src_bg,0,0,0,0,$bg_width,$new_bg_height,$width_orig,$height_orig);
	imagecopyresampled($tmp_sm,$src_sm,0,0,0,0,$sm_width,$new_sm_height,$width_orig,$height_orig);

//BIG: write the resized image to disk.
//$filename contains the path and the image name
$filename_bg = "../../../photos/misc/bigphotos/". $bg_file;
//0-100 is quality setting
imagejpeg($tmp_bg,$filename_bg,100);

//SMALL: write the resized image to disk.
//$filename contains the path and the image name
$filename_sm = "../../../photos/misc/smallphotos/". $sm_file;
//0-100 is quality setting
imagejpeg($tmp_sm,$filename_sm,100);
?>

 

i dont really see a difference between ours at a quick glance but this does work (only for a horizontal image though)

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.