SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Image Upload and Resize
-
Mar 2, 2005, 17:47 #1
- Join Date
- Feb 2004
- Location
- Ontario
- Posts
- 26
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Image Upload and Resize
Hi Everyone
I have a script here that will upload a image to the server.. what i want it to be able to do is resize it before it upload it so if i upload a image that is 500x500 it will be come 250x250, does that make since??
Please help me i'm going crazy.
Here is my upload Script
PHP Code:<?php
//echo $imgfile_name;
$dir="../testimages/";
$ifile = $dir . $imgfile_name;
$ext = strrchr($imgfile_name,'.');
echo $ifile;
if (isset($submit))
{
$fileexists = "n";
while ($fileexists != "y")
{
copy($imgfile,$ifile);
$fileexists = "y";
}
if (!is_uploaded_file ($imgfile))
{
echo "<b>$imgfile_name</b> couldn't be copied !!";
}
}
exec("mogrify -geometry 250x$tn_height! -format jpg $imgfile");
echo "<img src=../testimages/$imgfile_name>";
?>
-
Mar 2, 2005, 18:42 #2
- Join Date
- Nov 2004
- Location
- Cornwall, UK
- Posts
- 686
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
By the way you can do this bit:
$fileexists = "n";
while ($fileexists != "y")
{
copy($imgfile,$ifile);
$fileexists = "y";
}
if (!is_uploaded_file ($imgfile))
{
echo "<b>$imgfile_name</b> couldn't be copied !!";
}
much more easily by just using the function move_uploaded_file
I'm not sure about using exec() to thumbnail an image, I always use the gd library and imagecopyresampled() to create a thumbnail. I can post the function if you still get no joy with this method.
-
Mar 2, 2005, 18:59 #3
- Join Date
- Feb 2005
- Location
- Bandung, Indonesia
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh God. I think I won't be reading questions like this only once in my life.
Well, this time it's better to have a code:
PHP Code:/**
* Uploads a photo for the specified user.
*
* This method does not delete the photo prior to uploading. It will throw a database error if the
* uploaded photo already exists.
*
* @access public
* @param string Username.
* @param string File name.
* @param string MIME type.
*/
/*public*/ function Upload($username, $file, $mime_type)
{
// sanity check
if (!file_exists($file)) return CSF::raiseError("File '$file' does not exist.");
// get size
$stat = stat($file);
$size = $stat['size'];
// sanity check on size
if ($size > $this->MaxSize)
return Client::Retry("Your photo file size is too big (%d bytes). Maximum file size is %d bytes.",
$size, $this->MaxSize);
// get width & height
$imgsize = @getimagesize($file);
if (!$imgsize) return Client::Retry("Invalid photo file format.");
if ($imgsize[2] != 2) return Client::Retry("We only accept files in JPEG format. Please upload a JPEG file.");
$width = $imgsize[0];
$height = $imgsize[1];
// contents
$contents = file_get_contents($file);
// thumbnail
// calculate size first
// calculate using t_height = ThumbHeight
$t_height = $this->ThumbHeight;
$t_width = round(($t_height / $height) * $width);
// if more than calculate using t_width = ThumbWidth
if ($t_width > $this->ThumbWidth) {
$t_width = $this->ThumbWidth;
$t_height = round(($t_width / $width) * $height);
}
/* OLD: INCORRECT
$ratio = $width / $height;
$t_ratio = $this->ThumbWidth / $this->ThumbHeight;
if ($ratio < $t_ratio) {
$t_height = $this->ThumbHeight;
$t_width = $this->ThumbWidth * ($ratio / $t_ratio);
} else {
$t_width = $this->ThumbWidth;
$t_height = $this->ThumbHeight * ($ratio / $t_ratio);
}*/
// load orig file
$orig = @imagecreatefromjpeg($file);
if (!$orig) return CSF::raiseError("Invalid file format.");
// create a new truecolor image
$thumb = imagecreatetruecolor($t_width, $t_height);
if (function_exists('imagecopyresampled')) {
imagecopyresampled($thumb, $orig, 0, 0, 0, 0, $t_width, $t_height, $width, $height);
} else {
imagecopyresized($thumb, $orig, 0, 0, 0, 0, $t_width, $t_height, $width, $height);
}
ob_start();
imagejpeg($thumb);
$t_contents = ob_get_contents();
ob_end_clean();
$t_size = strlen($t_contents);
$t_mime_type = 'image/jpeg';
include_once 'Util/DateTime.class.php';
// insert into photo
$db =& CSF::GetDBConn();
$sql = 'INSERT INTO photos (username, size, width, height, mime_type, contents, time_upload)
VALUES (?, ?, ?, ?, ?, ?, ?)';
$pq = $db->prepare($sql);
$params = array($username, $size, $width, $height, $mime_type, $contents, DateTime::ToSQL());
$res = $db->executeParams($pq, NULL, $params);
if (CSF::isError($res)) return CSF::raiseError("Cannot execute INSERT query.");
$db->freePrepared($pq);
// insert into photothumb
$db =& CSF::GetDBConn();
$sql = 'INSERT INTO photothumbs (username, size, width, height, mime_type, contents, time_upload)
VALUES (?, ?, ?, ?, ?, ?, ?)';
$pq = $db->prepare($sql);
$params = array($username, $t_size, $t_width, $t_height, $t_mime_type, $t_contents, DateTime::ToSQL());
$res = $db->executeParams($pq, NULL, $params);
if (CSF::isError($res)) return CSF::raiseError("Cannot execute INSERT query.");
$db->freePrepared($pq);
return TRUE;
}
I hope the code is quite well-structured and commented so you can learn from it easily and apply it to your own implementation. You can also see the "math" to compute the size of the thumbnail to a predefined fixed size and maintaining ratio... etc. etc.Last edited by ceefour; Mar 4, 2005 at 23:13. Reason: Added PHP tags (just know this thing)
Hendy Irawan - Ruby on Rails Web Development
-
Mar 3, 2005, 09:32 #4
- Join Date
- Feb 2004
- Location
- Ontario
- Posts
- 26
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi There
I always use the gd library and imagecopyresampled() to create a thumbnail. I can post the function if you still get no joy with this method.
PHP Code:<?php
phpinfo();
?>
What are your thoughts?
-
Mar 3, 2005, 09:40 #5
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Without gd or imagemagik there isn't much you can do with images on upload.
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Mar 3, 2005, 09:43 #6
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
note: ceefour, use the proper code tags for posting code,
[ p h p ] [ / p h p ]
obviously without the spaces...
it makes it alot easier to read.
Cheers
SpikeZMike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Mar 3, 2005, 10:08 #7
- Join Date
- Feb 2004
- Location
- Ontario
- Posts
- 26
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey Thanks Man
I will try and remember
note: ceefour, use the proper code tags for posting code,
If I may ask what is the differecne between gd and iamgemagik?? what do you think is most effective and easy to learn and understand..
-
Mar 4, 2005, 23:20 #8
- Join Date
- Feb 2005
- Location
- Bandung, Indonesia
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
gd is bundled with PHP since version... phew! well it has been there for quite a long time.
Even the PHP source bundles it (it requires libjpeg externally though), but this shouldn't be your concern.
Anyways, gd is an extension so it's not loaded by default. You should be able to change your PHP configuration to include gd or any other extension you're interested in. If you can't do that I suggest moving to a different webhoster. Really, I'm serious. Even worse if your webhoster forces safe_mode. :-P
Even if you can't do so you can try dl('gd.so') or dl('php_gd.dll') (I'm not sure about the exact name in UNIX PHP) to see if you can load it dynamically. After that dump phpinfo() and you'll be able to see gd there, assuming the dl() call didn't give you an error, that is.
gd is *VERY* extensively used in many servers which do any kind of image processing, even the relatively simple task of thumbnailing something. ImageMagick is less used because it's an "external" program/library and it's not bundled with PHP.
Go http://www.php.net/ImageHendy Irawan - Ruby on Rails Web Development
Bookmarks