npsari Posted March 22, 2008 Share Posted March 22, 2008 Hi there In my website, users are allowed to upload 1 image but some people upload huge images, which slows down everything Is there anyway that when someone uploads an image, the image is shrinked automatically and converted to gif or something I think in Myspace for example, when i upload a huge image, something happens to it, and becomes smaller, with a bad resolution does anyone know how can i do this in my site Is there a way to convert hige images upon upload Thanks Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/ Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 Of course you can resize / thumbnail images. Check imagecopyresampled() or just google and find a million resizing/thumbnail scripts. Orio. Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498326 Share on other sites More sharing options...
BlueSkyIS Posted March 22, 2008 Share Posted March 22, 2008 i suggest saving yourself some time by using available class(es). here is the one i use: http://www.phpclasses.org/browse/package/4268.html Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498328 Share on other sites More sharing options...
AdRock Posted March 22, 2008 Share Posted March 22, 2008 This is what i use It creates a thumbnail and a larger image and also inserts image name into database <?php $idir = "../images/gallery/full/"; // Path To Images Directory $tdir = "../images/gallery/thumbs/"; // Path To Thumbnails Directory $twidth = "100"; // Maximum Width For Thumbnail Images $theight = "75"; // Maximum Height For Thumbnail Images $ldir = "../images/gallery/large/"; // Path To Thumbnails Directory $lwidth = "400"; // Maximum Width For Thumbnail Images $lheight = "300"; // Maximum Height For Thumbnail Images $pic=($_FILES['imagefile']['name']); $gallery = $_POST['gallery']; if (!isset($_POST['gallery'])) { ?> <fieldset> <legend><b>Image Gallery Upload</b></legend> <form enctype="multipart/form-data" action="<? $_SERVER['PHP_SELF']; ?>" method="post"> <p style="margin-left:10px;">Gallery to upload to:<br /> <select name="gallery" size="1"> <option value="jack">Jack</option> <option value="honeylands">Honeylands</option> <option value="events">Events</option> <option value="art">Art Auction</option> </select> <p style="margin-left:10px;">Image to upload:<br /> <input type="file" name="imagefile" style="width:450px" /><br /> <P><input type="submit" name="submit" value="Upload Image" class="submit-button" style="margin-left:10px;" /></p> </form> </fieldset><br /> <?} else { // Uploading/Resizing Script $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image //insert the image names into the database include_once "../includes/connection.php"; $query = "INSERT INTO image VALUES ('','$pic','$pic','$gallery')"; mysql_query($query); mysql_close(); $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if ($currheight > $currwidth) { // If Height Is Greater Than Width $zoom = $twidth / $currheight; // Length Ratio For Width $newheight = $theight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width } else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if ($currheight > $currwidth) { // If Height Is Greater Than Width $zoom = $lwidth / $currheight; // Length Ratio For Width $newheight = $lheight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width } else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $lwidth / $currwidth; // Length Ratio For Height $newwidth = $lwidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$ldir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } } ?> Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498330 Share on other sites More sharing options...
npsari Posted March 22, 2008 Author Share Posted March 22, 2008 ohh, nice guys I noticed that these codes show how to resize an image, but what about file size Does it also result in smaller file size because a bmp image can still be heavy even if it is not so big Do you see what i mean Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498331 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 Smaller image -> Smaller size. Convert all images to JPG -> Smaller size. Orio. Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498334 Share on other sites More sharing options...
npsari Posted March 22, 2008 Author Share Posted March 22, 2008 is that the part which converts it to JPG imagejpeg($image_p, null, 100); Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498337 Share on other sites More sharing options...
Orio Posted March 22, 2008 Share Posted March 22, 2008 imagejpeg() creates a JPG, yes. Orio. Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498348 Share on other sites More sharing options...
npsari Posted March 22, 2008 Author Share Posted March 22, 2008 ok, great i will work on it thanks everyone and thanks Orio Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-498356 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 I am a little confused Do i use imagecopyresampled() to save the image in a directory (after being shrinked) or i allow users to upload the original size normally, then i use imagecopyresampled() to echo the image uploaded Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-499273 Share on other sites More sharing options...
samshel Posted March 24, 2008 Share Posted March 24, 2008 depends on your choice but i would say saving original image some where on server is always a good idea, although in the site you would always use the thumbnail. You can use the stored image to generate different size thumb nails if needed in future and you woudlnt have to ask the user to upload it again. hth Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-499275 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 wouldnt that count as loading the whole image I mean, if i save the original image size, then i show half of it using imagecopyresampled(), isnt that the same as showing the whole image please, what does that part mean in the code, can i cancel it? // Content type header('Content-type: image/jpeg'); Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-499281 Share on other sites More sharing options...
samshel Posted March 24, 2008 Share Posted March 24, 2008 that wouldnt count as loading the image as you would be showing only the thumbnail on your site, which is smaller in size. The original image would not be shown on the site. Content type header specifies what type of file is being displayed. Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-499288 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 solved it, thank you Link to comment https://forums.phpfreaks.com/topic/97385-images-uploaded-in-my-site-are-hugeeee/#findComment-499294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.