svgmx5 Posted October 29, 2009 Share Posted October 29, 2009 Not sure if this is the right place, but i was wondering if there was a way to optomize an image using php? a way to automatically lower the image quality like a jpg from 100 to 79 or so. Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/ Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 Yeah, load it using GD and then use the quality parameter for imagejpeg. Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-946875 Share on other sites More sharing options...
svgmx5 Posted October 29, 2009 Author Share Posted October 29, 2009 thanks daniel. I went ahead and checked it my server had the gp enable, it did. so i went ahead and did a test script. But when i view the page in the browser, all i see is a broken image sign and nothing else. Not sure what i'm doing wrong... Here is the code i'm using: <?php if($dh = opendir("images/gallery/test/")){ $img = "images/gallery/test/Dock.jpg"; header('Content-type: image/jpeg'); imagejpeg($img, NULL, 75); imagedestroy($img); closedir($dh); } echo 'This is a test to see if the </br.> <img src="'.$img.'"/> <br/> is actually optomized '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-947053 Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 That's not how it works. $img = imagecreatefromjpeg('images/gallery/test/Dock.jpg'); header('Content-type: image/jpeg'); imagejpeg($img, null, 75); imagedestroy(); imagejpeg() outputs the image. Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-947064 Share on other sites More sharing options...
svgmx5 Posted October 29, 2009 Author Share Posted October 29, 2009 thanks for that. It worked. Now i'm stuck at another point. What I'm trying to do is optimize the images from a directory. What i have working so far is displaying images as thumbs from a directory, since the images are big files i want to optimize them so the thumbs can load up faster. So what i did was i tried to plug the script that just worked into the script i allready had. i wasn't sure if that would work, and so far it hasn't. Not sure if i have to put it in before the array starts or how i would. Anyway here is the full script that i have right now that. <?php $path = "images/gallery/test/"; # Location of small versions $height = 75; //height for each thumbnail $width = 75; //the width for each thumbnail $cols = 10; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { $files[] = $file; } } closedir($handle); } $colCtr = 0; if(empty($files)){ echo"You currently have no images in this folder. To add some please upload them using the"; }else{ echo'<form method="post" action="openDirectory.php">'; echo'<table width="800" cellspacing="0" cellpadding="5" border="0">'; foreach($files as $file){ //Here is were the optimization would begin.. $img = imagecreatefromjpeg($file); imagejpeg($img, null, 75); imagedestroy($img); if($colCtr %$cols == 0) echo '<tr>'; echo ' <td> <img src="'.$path.$img.'" height="'.$height.'" width="'.$width.'" /><br/> <input type="checkbox" name="chkFiles[]" value="'.$img.'"/> </td> '; $colCtr++; } echo'</tr></table>'."\r\n"; echo'<input type="submit" name="submit" value="Deleted Checked"/>'; echo'</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-947113 Share on other sites More sharing options...
Daniel0 Posted October 29, 2009 Share Posted October 29, 2009 No, you cannot do that. As I said, imagejpeg outputs the image when called with a null filename. Try opening an image in notepad (or another plain text editor); that's the kind of stuff it'll output. It doesn't return a path or anything along those lines. The only thing it will ever return is boolean indicating either failure or success. You can use the second parameter to have it save the image to a file instead. In that way you can do what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-947149 Share on other sites More sharing options...
svgmx5 Posted October 29, 2009 Author Share Posted October 29, 2009 got it. well thanks allot for your help. I think i got it from here then, but i have any problems i'll return again. Quote Link to comment https://forums.phpfreaks.com/topic/179459-solved-can-an-image-be-optomized-using-php/#findComment-947163 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.