Jump to content

reduce image file size


Craig Hopson
 Share

Recommended Posts

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

  • Like 1
Link to comment
Share on other sites

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

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

  • Like 1
Link to comment
Share on other sites

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
Thanks this may just do it!!
Link to comment
Share on other sites

Guest So Called

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

  • 2 weeks later...

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

it would be a 100Mb web page????
Right.
is there a way to reduce quality on the fly
You 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.
  • Like 1
Link to comment
Share on other sites

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 by Craig Hopson
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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????

You have a 1mb 100x100 picture? That's terrible even for bitmap.
Link to comment
Share on other sites

Guest So Called

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.

  • Like 1
Link to comment
Share on other sites

<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
Scrap 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

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

Guest So Called
The thumbnail.php script I did try but it seem to take long time to load
Just 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

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 by Craig Hopson
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...