robcrozier Posted May 17, 2007 Share Posted May 17, 2007 Hi, ok as far as im aware there is a method which enables you to reduce the file size of an image using PHP. This is obviously great for creating smaller sized, fast loading thumbnails of a larger image without having to manipulate the image yourself at all. Great for people who dont really know anything about image manipulation too! Ok then so my question is: "Does anyone know how this is done!?" Or can anyone suggest a better way to do this without having to manipulate the image yourself and then upload it to the server before it can be accessed. cheers! Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/ Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 Use imagecopyresampled either edit the code to create a new file or use as is image.php <?php // The file $filename = $_GET['img']; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Output imagejpeg($image_p, null, 100); ?> use as <img src='image.php?img=test.jpg' /> Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255207 Share on other sites More sharing options...
robcrozier Posted May 17, 2007 Author Share Posted May 17, 2007 Hi mate, ive tried what you suggested however the outputted image will not display, as if im calling an image which is not on the server (the white square with the red cross inside)??? ive also tried moving the whole image file inside my images folder and calling it from there, not working! any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255217 Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 First check the first line of image.php is <?php can you post or pm a link to your site (if online) i'll take a quick look Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255220 Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 other tests to try are put test.jpg in the same folder and then try http://mydomain.com/mysite/test/image.php?img=test.jpg if this fails comment the line //header('Content-type: image/jpeg'); try again you may see alot of gook appear but at the top you may see a error with the file path Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255222 Share on other sites More sharing options...
robcrozier Posted May 17, 2007 Author Share Posted May 17, 2007 Unfortunately mate its on my localhost, the first line of image.php is <? however? here is exactly what i am doing: image.php <? ob_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php // The file $filename = $_GET['img']; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Output imagejpeg($image_p, null, 100); ?> </body> </html> reduce_image.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <img src='image.php?img=banner.jpg' /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255224 Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 Ahh no no the whole page is <?php // The file $filename = $_GET['img']; $percent = 0.5; // Content type header('Content-type: image/jpeg'); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Output imagejpeg($image_p, null, 100); ?> Nothing more nothing less remove the <html> etc etc etc Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255226 Share on other sites More sharing options...
robcrozier Posted May 17, 2007 Author Share Posted May 17, 2007 SMASHING MATE! Thats done the trick! thanks a lot! : Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255230 Share on other sites More sharing options...
Trium918 Posted May 17, 2007 Share Posted May 17, 2007 Is the GD Graphics Library required in order for this to work. Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255305 Share on other sites More sharing options...
MadTechie Posted May 17, 2007 Share Posted May 17, 2007 Yes it is Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255310 Share on other sites More sharing options...
Trium918 Posted May 17, 2007 Share Posted May 17, 2007 Yes it is Damn! Thanks man. Quote Link to comment https://forums.phpfreaks.com/topic/51799-solved-reducing-an-image-file-size/#findComment-255311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.