Jump to content

[SOLVED] Can an image be optomized using php?


svgmx5

Recommended Posts

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

 

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>';
}
?>

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.