n8dog
asked on
GD Image Resizing
im looking to resize images that are uploaded to a basic standard.
Example:
User A Uploads an image that is 2MB, and the proposed function resizes the image to 160 Width, leaving the Height (keeping the aspect ratio), then Converting it to a LOW Quality (10-20%) JPG, then the SQL upload code continues to insert it into the DB.
So Resize
Resample
Present for DB Query
Anyone got a class that easily handles this or something?
I'm running PHP 4.4.4 & mySQL btw
-n
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($neww idth, $newheight);
$source = imagecreatefromjpeg($filen ame);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
//imagejpeg($thumb);
$fp = fopen($thumb, 'r');
$content = fread($fp, filesize($thumb));
$content = addslashes($content);
fclose($fp);
$query = "UPDATE datingpics SET name='$fileName', size='$fileSize', type='$fileType', content='$content' WHERE userid='$uid'";
?>
other details u will find at
http://in2.php.net/manual/en/function.imagecopyresized.php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($neww
$source = imagecreatefromjpeg($filen
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
//imagejpeg($thumb);
$fp = fopen($thumb, 'r');
$content = fread($fp, filesize($thumb));
$content = addslashes($content);
fclose($fp);
$query = "UPDATE datingpics SET name='$fileName', size='$fileSize', type='$fileType', content='$content' WHERE userid='$uid'";
?>
other details u will find at
http://in2.php.net/manual/en/function.imagecopyresized.php
ASKER
So this will permanently resize the image uploaded?
if so, awesome!
I will try this code out. - thanks!
n8Dog
if so, awesome!
I will try this code out. - thanks!
n8Dog
ASKER
is this resizing, or just viewing it resized?
ASKER
i tried this code, it doesnt work.
looks like a direct copy & paste from the link you posted.
-n
looks like a direct copy & paste from the link you posted.
-n
hi
for me the script works fine. please check if php has GD liberary enabled. use phpinfo().
to get this script running php must support GD liberary.
it is resizing the image. u can check that by checking the the size of the thumb file.
the thumb image will be distorted since GD doesn't do a perfect resize.
so i will suggest use imagemagik for resizing image.
regards
anilande
for me the script works fine. please check if php has GD liberary enabled. use phpinfo().
to get this script running php must support GD liberary.
it is resizing the image. u can check that by checking the the size of the thumb file.
the thumb image will be distorted since GD doesn't do a perfect resize.
so i will suggest use imagemagik for resizing image.
regards
anilande
ASKER
yes, it is enabled.
what is image magik?
what is image magik?
it an opensource application most of the webhosting providers provide service of imagemagic because it does a perfect resize of an image.
can u tell me what error it is throwing??
anilande
can u tell me what error it is throwing??
anilande
ASKER
Here is the complete code I am using. I tried to integrate what you had, but i might have not gotten it right.
can you integrate your resize solution with this?
code
-------------------------- ---------- ---------
// variables
$upload = $_POST['profilepics_image_ upload'];
$profilepics_ownerid = $_SESSION['userid'];
//picture upload portion ########################## ########## ###
?> <center> <?
if( ($upload) && ($_FILES['profilepics_imag e']['size' ]!=0) ){
if( ($upload) && ($_FILES['profilepics_imag e']['size' ]<50000) ){
$profilepics_image_name = $_FILES['profilepics_image ']['name'] ;
$tmpName = $_FILES['profilepics_image ']['tmp_na me'];
$profilepics_image_size = $_FILES['profilepics_image ']['size'] ;
$profilepics_image_type = $_FILES['profilepics_image ']['type'] ;
$fp = fopen($tmpName, 'r');
$profilepics_image = fread($fp, filesize($tmpName));
$profilepics_image = addslashes($profilepics_im age);
fclose($fp);
if(!get_magic_quotes_gpc() ){
$profilepics_image_name = addslashes($profilepics_im age_name);
}
// check for existing image
$sql_imagechk = mysql_query("SELECT imagename, imagetype, imagesize, image FROM profilepics WHERE ownerid = '$profilepics_ownerid'") or die (mysql_error());
$numimages = mysql_numrows($sql_imagech k);
if( $numimages==0 ){
// insert picture
$sql_imageinsert = mysql_query("INSERT INTO profilepics (ownerid, image, imagetype, imagesize, imagename) VALUES('$profilepics_owner id', '$profilepics_image', '$profilepics_image_type', '$profilepics_image_size', '$profilepics_image_name') ") or die (mysql_error());
}elseif( $numimages==1){
// update picture
$sql_imageupdate = mysql_query("UPDATE profilepics SET image='$profilepics_image' , imagetype='$profilepics_im age_type', imagesize='$profilepics_im age_size', imagename='$profilepics_im age_name' WHERE ownerid='$profilepics_owne rid'") or die (mysql_error());
}
//$query = "UPDATE profilepics SET imagename='$profile_image_ name', imagesize='$profile_image_ size', imagetype='$profile_image_ type', image='$profile_image' WHERE ownerid='$uid'";
//mysql_query($query) or die('Error, Update Failed!');
echo "<br><h2>Picture $profilepics_image_name Saved/Updated Successfully</h2><br> <input type=button value=Continue onclick=history.back() class=btn> ";
}elseif( ($upload) && ($_FILES['profilepics_imag e']['size' ]>50000) ){
$profile_image_name = $_FILES['profilepics_image ']['name'] ;
$profile_image_size = $_FILES['profilepics_image ']['size'] ;
?>
<hr width=450 color=black>
<h2>
The file size of the image you tried to Save was TOO LARGE</h2> <br>
<h3>
File Name: <font color=black><? echo $profilepics_image_name; ?></font> File Size: <? echo substr($profilepics_image_ size,0,3). 'KB'; ?><br><br>
The picture must be under 50KB - Your picture was: <? echo substr($profilepics_image_ size,0,3). 'KB'; ?><br><br>
Please Reduce and Try Again
<br><br>
<input type="button" value="Continue" onclick="history.back()" class="btn">
</h3>
<?
}
}
?> </center> <?
//end picture upload ########################## ########## ###
can you integrate your resize solution with this?
code
--------------------------
// variables
$upload = $_POST['profilepics_image_
$profilepics_ownerid = $_SESSION['userid'];
//picture upload portion ##########################
?> <center> <?
if( ($upload) && ($_FILES['profilepics_imag
if( ($upload) && ($_FILES['profilepics_imag
$profilepics_image_name = $_FILES['profilepics_image
$tmpName = $_FILES['profilepics_image
$profilepics_image_size = $_FILES['profilepics_image
$profilepics_image_type = $_FILES['profilepics_image
$fp = fopen($tmpName, 'r');
$profilepics_image = fread($fp, filesize($tmpName));
$profilepics_image = addslashes($profilepics_im
fclose($fp);
if(!get_magic_quotes_gpc()
$profilepics_image_name = addslashes($profilepics_im
}
// check for existing image
$sql_imagechk = mysql_query("SELECT imagename, imagetype, imagesize, image FROM profilepics WHERE ownerid = '$profilepics_ownerid'") or die (mysql_error());
$numimages = mysql_numrows($sql_imagech
if( $numimages==0 ){
// insert picture
$sql_imageinsert = mysql_query("INSERT INTO profilepics (ownerid, image, imagetype, imagesize, imagename) VALUES('$profilepics_owner
}elseif( $numimages==1){
// update picture
$sql_imageupdate = mysql_query("UPDATE profilepics SET image='$profilepics_image'
}
//$query = "UPDATE profilepics SET imagename='$profile_image_
//mysql_query($query) or die('Error, Update Failed!');
echo "<br><h2>Picture $profilepics_image_name Saved/Updated Successfully</h2><br> <input type=button value=Continue onclick=history.back() class=btn> ";
}elseif( ($upload) && ($_FILES['profilepics_imag
$profile_image_name = $_FILES['profilepics_image
$profile_image_size = $_FILES['profilepics_image
?>
<hr width=450 color=black>
<h2>
The file size of the image you tried to Save was TOO LARGE</h2> <br>
<h3>
File Name: <font color=black><? echo $profilepics_image_name; ?></font> File Size: <? echo substr($profilepics_image_
The picture must be under 50KB - Your picture was: <? echo substr($profilepics_image_
Please Reduce and Try Again
<br><br>
<input type="button" value="Continue" onclick="history.back()" class="btn">
</h3>
<?
}
}
?> </center> <?
//end picture upload ##########################
where are u resizing the image?
ASKER
i posted the version that IS NOT incorporating the code you provided.
when i incorporated your solution, the content uploaded into the BLOB in my DB was blank...
:(
when i incorporated your solution, the content uploaded into the BLOB in my DB was blank...
:(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry chenage the resize function in the commented section with this one
Resize($tmpName,$tmpName,$ newwidth,$ newheight, 100);
Resize($tmpName,$tmpName,$
ASKER
instead of the percentage setting, is it possible to absolute size it. For example: Absolute: Width=140, Height=variable ?
basically what i have is a situation where users upload images, but i need them to always be resized to a standard.
the exact standard is 140 x 160 (w x h), however anything 140 x Z is fine
understand?
basically what i have is a situation where users upload images, but i need them to always be resized to a standard.
the exact standard is 140 x 160 (w x h), however anything 140 x Z is fine
understand?
update following variables
ex..
$newwidth = 500;
$newheight = 600;
ex..
$newwidth = 500;
$newheight = 600;
ASKER
GREAT!
it works!
I have a question though.. i would like to maintain the Aspect Ratio... anf just resize the Width to 140, and let the Height adjust automatically to the size ratio relative to 140.
Know how?
-n8
it works!
I have a question though.. i would like to maintain the Aspect Ratio... anf just resize the Width to 140, and let the Height adjust automatically to the size ratio relative to 140.
Know how?
-n8
thats what we are doing here we are mentioning that the image should be resixed to
if $newwidth>500
image size must = 500 X z
and if newheight >600
image size must = z X 600
where z=relative other dimention
we are keeping the aspect ration.
anilande
if $newwidth>500
image size must = 500 X z
and if newheight >600
image size must = z X 600
where z=relative other dimention
we are keeping the aspect ration.
anilande
sorry i didn't check the dimentions
just change the $newwidth=140 in place of 500
and for your condition
put function statement as
$newwidth = 140;
$newheight =1000;
Resize($tmpName,$tmpName,$ newwidth,$ newheight, 100);
anilande
just change the $newwidth=140 in place of 500
and for your condition
put function statement as
$newwidth = 140;
$newheight =1000;
Resize($tmpName,$tmpName,$
anilande
ASKER
So use this?
list($width, $height) = getimagesize($tmpName);
// REMOVED $percent = 0.5;
$newwidth = 200;
$newheight = 1000;
Resize($tmpName,$tmpName,$ newwidth,$ newheight, 80);
-n
list($width, $height) = getimagesize($tmpName);
// REMOVED $percent = 0.5;
$newwidth = 200;
$newheight = 1000;
Resize($tmpName,$tmpName,$
-n
ASKER
im trying this code block out, and it's really working GREAT!
i really appreciate your diligence in the matter.
:)
-n8
i really appreciate your diligence in the matter.
:)
-n8
thanks :)
ASKER
Im looking to integrate a RESIZER into this.
The Height is to be hard coded to 160px.
Code
--------------------------
//picture upload portion ##########################
if( ($upload) && ($_FILES['userfile']['size
if( ($upload) && ($_FILES['userfile']['size
$fileName = $_FILES['userfile']['name'
$tmpName = $_FILES['userfile']['tmp_n
$fileSize = $_FILES['userfile']['size'
$fileType = $_FILES['userfile']['type'
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc()
$fileName = addslashes($fileName);
}
$query = "UPDATE datingpics SET name='$fileName', size='$fileSize', type='$fileType', content='$content' WHERE userid='$uid'";
mysql_query($query) or die('Error, Update Failed!');