Hi
Does anyone know a good script and method for resizing an image at the point of upload?
I have found a few that resize to the browser, but I want to do this for Avatars. So someone could upload a 500kb big picture, but it reduces the physical size to around 150px150px.
I find 'functions' but haven't a clue how to incorporate them into a PHP $row->photo type code.
Please help.
Image Resizing
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Image Resizing
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Re: Image Resizing
simon programmers don't use 'good script' but i do know of a method.
using the gd library you can resize an image, specifically with the function imagecopyresampled()
http://php.net/manual/en/book.image.php
using the gd library you can resize an image, specifically with the function imagecopyresampled()
http://php.net/manual/en/book.image.php
Re: Image Resizing
Try below given function.
Code: Select all
function pictureResize($save_directory, $file_array, $max_width, $max_height, $postid)
{
//--change filename to time - make it unique
$temp_filename = strtolower($file_array['name']);
$ext = substr(strrchr($temp_filename, "."), 1);
$temp_filename = $postid.strrchr($file_array['name'],".");
//--check that it's a jpeg or gif
if (preg_match('/^(gif|jpg|jpeg)$/',$ext))
{
// resize in proportion
list($width_orig, $height_orig) = getimagesize($file_array['tmp_name']);
if ($max_width && ($width_orig < $height_orig))
{
$max_width = ($max_height / $height_orig) * $width_orig;
}
else
{
$max_height = ($max_width / $width_orig) * $height_orig;
}
$image_p = imagecreatetruecolor($max_width, $max_height);
//handle gifs and jpegs separately
if($ext=='gif')
{
$image = imagecreatefromgif($file_array['tmp_name']);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $max_width, $max_height, $width_orig, $height_orig);
imagegif($image_p, $save_directory.$temp_filename, 80);
}
else
{
$image = imagecreatefromjpeg($file_array['tmp_name']);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $max_width, $max_height, $width_orig, $height_orig);
imagejpeg($image_p, $save_directory.$temp_filename, 80);
}
imagedestroy($image_p);
imagedestroy($image);
return $temp_filename;
}
else
{
$error_message.="<br> file is not a jpeg or gif picture <br>";
}
}
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Image Resizing
Thanks for both options. I will look at both.
PBS: I don't know how to "perform a function" from within a web page as I have never written functions in that way.
For example: when someone uploads an image, this is my script:
I don't have the knowledge to know how I place a function within that, so it only runs when $update" has "photoadd" in it.
PBS: I don't know how to "perform a function" from within a web page as I have never written functions in that way.
For example: when someone uploads an image, this is my script:
Code: Select all
if($update == "photoadd")
{
$pic=($_FILES['photo']['name']);
if ($pic != NULL)
{
$target = $_SERVER['DOCUMENT_ROOT']."/beta/images/avatars/";
srand(time());
$random = (rand()%99999999);
$pic=($_FILES['photo']['name']);
$newname="$random"."$pic";
$target = $target . $newname;
mysql_query("UPDATE users SET photo = '$newname' where id = '$cookieid'");
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "<div class='admincompletedbox'>
<b>Your Avatar has been added. <img src='images/iconimage.jpg' /></b></div>";
}
}
}Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Image Resizing
How would I incorporate this:
imagejpeg($image_p, null, 100);
Into my image upload code?
This appears to load the image to the browser. Also - when I used the code in this page (http://www.php.net/manual/en/function.i ... ampled.php), and places my image file string into the top variable, all it did was report the current URL to the browser and nothing else.
Very odd.
imagejpeg($image_p, null, 100);
Into my image upload code?
This appears to load the image to the browser. Also - when I used the code in this page (http://www.php.net/manual/en/function.i ... ampled.php), and places my image file string into the top variable, all it did was report the current URL to the browser and nothing else.
Very odd.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.