Craig Hopson Posted May 30, 2012 Share Posted May 30, 2012 ok so users can upload photos to my site the problem i have is space i'm cool with photo's upto 1 Mb but not any bigger how can i reduce the file size,the other problem i have is file type it could be any of the following jpeg, bmp, png or gif, I have searched Google for about an hour with no luck... Thanks 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted May 30, 2012 Share Posted May 30, 2012 You need to resize the image to decrease the file size. the other problem i have is file type it could be any of the following jpeg, bmp, png or gif, I have searched Google for about an hour with no luck...What exactly is your question? Link to comment Share on other sites More sharing options...
Craig Hopson Posted May 30, 2012 Author Share Posted May 30, 2012 How can i reduce image file size using PHP Link to comment Share on other sites More sharing options...
birbal Posted May 30, 2012 Share Posted May 30, 2012 or you can lower the quality of the image using third parameter of imagejpeg() and imagepng() gif has not that option and bmp format cant be compressed. though you can convert those to jpeg and png and lower the quality. wahtever suits your situation 1 Link to comment Share on other sites More sharing options...
birbal Posted May 30, 2012 Share Posted May 30, 2012 How can i reduce image file size using PHPyou need to use http://php.net/gd2 Link to comment Share on other sites More sharing options...
Craig Hopson Posted May 30, 2012 Author Share Posted May 30, 2012 or you can lower the quality of the image using third parameter of imagejpeg() and imagepng() gif has not that option and bmp format cant be compressed. though you can convert those to jpeg and png and lower the quality. wahtever suits your situationThanks this may just do it!! Link to comment Share on other sites More sharing options...
Guest So Called Posted May 30, 2012 Share Posted May 30, 2012 I presume you want to edit the images to smaller images by means of a script on the server side. You can probably do it using GDI, but I doubt the job will be easy: http://php.net/manual/en/ref.image.phphttp://php.net/manual/en/book.image.php And one word to the wise, make sure your host supports GDI before you spend much time looking into it. You can find out using php_info(). Link to comment Share on other sites More sharing options...
Guest So Called Posted May 30, 2012 Share Posted May 30, 2012 You beat me to it Birbal, you were answering while I was checking out the source links before replying. Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 13, 2012 Author Share Posted June 13, 2012 if i had 100 photos on my site all 1Mb in size and displayed them as thumbnails by this method echo '<img src="'.$row['img'].'" height="100px" width="100px">'; it would be a 100Mb web page????is there a way to reduce quality on the fly i tried $im = imagecreatefromjpeg($row['img']);imagejpeg($im, NULL, 60); but this just outputs the LOADS of random code.... Link to comment Share on other sites More sharing options...
justsomeguy Posted June 13, 2012 Share Posted June 13, 2012 it would be a 100Mb web page????Right. is there a way to reduce quality on the flyYou can use a PHP resize script as the src for the image if you want to have your server resize 100MB of images every time someone requests a page. It may be more efficient to create the thumbnail when the image gets uploaded instead of dynamically resizing everything every time someone requests a page. but this just outputs the LOADS of random code....It's not exactly "random", but yes it will display the binary data if you don't send headers to tell the browser that it's an image. 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 13, 2012 Author Share Posted June 13, 2012 (edited) i found this function ReduceImage($img){$img = file_get_contents ("$img");$im = imagecreatefromstring ($img);header ("Content-Type: image/jpeg"); imageJPEG ($im,NULL,10);imageDestroy ($im);} but when i use it on my site i get cant modify headers and if i remove the "header ("Content-Type: image/jpeg");" line i get the binary data back Edited June 13, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
birbal Posted June 13, 2012 Share Posted June 13, 2012 you have to make separate page for this which you will link to src attribute of img tag. you cant send two content type header for one page. the error is showing cause it already sent the headers with the first output of the text whixh is by default content-type of text/html. so you cant sent image headers again 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 13, 2012 Author Share Posted June 13, 2012 Thanks guys you've done it again and answered my questions Thank you :Happy: Link to comment Share on other sites More sharing options...
roabecker Posted June 13, 2012 Share Posted June 13, 2012 if i had 100 photos on my site all 1Mb in size and displayed them as thumbnails by this methodecho '<img src="'.$row['img'].'" height="100px" width="100px">'; it would be a 100Mb web page???? You have a 1mb 100x100 picture? That's terrible even for bitmap. Link to comment Share on other sites More sharing options...
Guest So Called Posted June 14, 2012 Share Posted June 14, 2012 Craig, it's very poor practice to have a large image and then force it to a thumbnail by overriding its IMG tag height and width. That's what you're doing, right? Actual image size much larger, right? If you have all the images (rather than images that get uploaded) then just open them in an image processing program and use the program's thumbnail generator, they almost all have that feature. If your image content is dynamic (changes in real time) then you'll need to write a PHP script that receives the image request and processes the full image into a thumbnail size. You could include the full size image name as a query string then use the script name instead of your image tag. Like this: <IMG SRC="thumbnail.php?img=full_image.jpg"> The script above could either grab the full size image and reduce it to thumbnail, or it could grab the image from a database and use that as source. I'm serving images from a MySQL database on my site but I would not recommend it if you use very much bandwidth. The images I'm serving are small size and infrequent access. 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 <IMG SRC="thumbnail.php?img=full_image.jpg"> do you have an example of this?The thumbnail.php script I did try but it seem to take long time to load Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 <IMG SRC="thumbnail.php?img=full_image.jpg"> do you have an example of this?The thumbnail.php script I did try but it seem to take long time to loadScrap this anyone got a function that takes the ordinal image changes it to thumbnail and saves the output to thumbnail directory Link to comment Share on other sites More sharing options...
birbal Posted June 14, 2012 Share Posted June 14, 2012 image*() has a option to specify a file name location to save the data in file. if it is not decreasing size as you want to. you can increase the compression levels. croping the images into smaller resolution also decrease the size of the image. the php page will be same as other php ,difference is it only serve image. so you can use $_GET as you used to with other html outputing page. Link to comment Share on other sites More sharing options...
Ingolme Posted June 14, 2012 Share Posted June 14, 2012 I wouldn't have PHP instantly generate a thumbnail, but rather generate the thumbnail and saving it upon uploading the picture. You could do that with this function: http://es2.php.net/manual/en/function.imagecopyresampled.php Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 I wouldn't have PHP instantly generate a thumbnail, but rather generate the thumbnail and saving it upon uploading the picture. You could do that with this function: http://es2.php.net/manual/en/function.imagecopyresampled.php YES thanks Link to comment Share on other sites More sharing options...
Guest So Called Posted June 14, 2012 Share Posted June 14, 2012 The thumbnail.php script I did try but it seem to take long time to loadJust run all the images through a photo processing program and create the thumbnails, then put them in a different directory. Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 (edited) ok i now have this function so when uploads are made it will reduce the image file size (a little) and create a thumbnail does it all look ok?? function converttojpg($originalimg){$filetype = @end(explode(".", $originalimg));if (strtolower($filetype) == 'jpg' or strtolower($filetype) == 'jpeg') { $srcImg = imagecreatefromjpeg($originalimg); imagejpeg($srcImg,$originalimg,75); imagedestroy($srcImg); }if (strtolower($filetype) == 'png') { $srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); $NewName = str_replace(".png",".jpg",$originalimg); rename($originalimg,$NewName); }if (strtolower($filetype) == 'gif') { $srcImg = imagecreatefromgif($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); $NewName = str_replace(".gif",".jpg",$originalimg); rename($originalimg,$NewName); } $thumb_img = imagecreatefromjpeg($NewName); $origw=imagesx($thumb_img);$origh=imagesy($thumb_img);$new_w = 120;$diff=$origw/$new_w;$new_h=$new_w;$dst_img = imagecreate($new_w,$new_h);imagecopyresized($dst_img,$thumb_img,0,0,0,0,$new_w,$new_h,imagesx($thumb_img),imagesy($thumb_img));$ONLYname = str_replace('uploads/'.$_COOKIE['id'].'/',"",$NewName);imagejpeg($dst_img, 'uploads/'.$_COOKIE['id'].'/thumbnail/'.$ONLYname);} Seems to be working ok only problem is the thumbnails are VERY poor quality any ideas?? Edited June 14, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 This is the quality i'm getting Link to comment Share on other sites More sharing options...
justsomeguy Posted June 14, 2012 Share Posted June 14, 2012 Another solution would be to have the thumbnail script only dynamically resize if the thumbnail doesn't already exist. That way you don't need to process the pictures when they're uploaded, the thumbnail would get created and saved the first time it's requested and after that it would just use the existing thumbnail. The third parameter for imagejpeg is the quality, the default quality is about 75. 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 14, 2012 Author Share Posted June 14, 2012 like a cron job? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now