Jump to content

resize image before caching


Guest

Recommended Posts

This script will load an image (jpg, gif or png) and then save a PNG local copy for caching.

 

I'm trying to find a way to resize the image to 300x300 before saving it as a PNG.

 

I tried to use the function imagecopyresampled() (as you can see in the commented lines) but i get an error saying the image could not be displayed because it contains errors.

header('Content-Type: image/png');

			$imgpochette = $_GET['i'];

			$ENABLE_CACHE = true;
			$CACHE_TIME_HOURS = 744;
			$CACHE_FILE_PATH = "pochette_album/$imgpochette.png";

			if($ENABLE_CACHE && file_exists($CACHE_FILE_PATH) && (time() - filemtime($CACHE_FILE_PATH) < ($CACHE_TIME_HOURS * 60 * 60))) {
			  echo @file_get_contents($CACHE_FILE_PATH);
			} else {
					// Load the requested image
					$imgdisplay = "http://[redacted]/pochette.php?i=$imgpochette&display=1";
					$image = imagecreatefromstring(file_get_contents($imgdisplay));
// $width = "30";
// $height = "30";
// $new_image = imagecreatetruecolor($width, $height);
// imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $image.getWidth(), $image.getHeight());
					// Send the image
					imagejpeg($image, $CACHE_FILE_PATH);
					exit();
			  @file_put_contents($CACHE_FILE_PATH, $output);
			  echo $output;
			}
Link to comment
https://forums.phpfreaks.com/topic/280553-resize-image-before-caching/
Share on other sites

I'm guessing the problem is this: $image.getWidth() and $image.getHeight()

 

As far as I know, that is not a valid way to get the dimensions of an image identifier (if you can get them at all from an identifier). Try using getimagesize() on the file ($CACHE_FILE_PATH), then use the returned values in the function.

 

 

list($originalWidth, $originalHeight) = getimagesize($CACHE_FILE_PATH);
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);

Thanks for the reply.

 

2 problems now :

 

- The image is saved as PNG in the cache folder, but isn't resized.

- The first time i will load the image, i will get an error (image cannot be displayed because it contains error) but the image will still be saved as PNG in the cache folder. Second time i load the image, it will be displayed correctly (using the cached version) but it isn't resized.

 

Here's the full code of my page. The first part is used to cache the image, the second part is used to display the non-cached image (it reads an image from a ZIP file and output the content without extracting anything)

 

 

pochette.php :

if (empty($_GET['display'])) {
header('Content-Type: image/png');

			$imgpochette = $_GET['i'];

			$ENABLE_CACHE = true;
			$CACHE_TIME_HOURS = 744;
			$CACHE_FILE_PATH = "pochette_album/$imgpochette.png";

			if($ENABLE_CACHE && file_exists($CACHE_FILE_PATH) && (time() - filemtime($CACHE_FILE_PATH) < ($CACHE_TIME_HOURS * 60 * 60))) {
			  echo @file_get_contents($CACHE_FILE_PATH);
			} else {
					// Load the requested image
					$imgdisplay = "http://[redacted]/pochette.php?i=$imgpochette&display=1";
					$image = imagecreatefromstring(file_get_contents($imgdisplay));
$width = "30";
$height = "30";
list($originalWidth, $originalHeight) = getimagesize($CACHE_FILE_PATH);
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
					// Send the image
					imagejpeg($image, $CACHE_FILE_PATH);
					exit();
			  @file_put_contents($CACHE_FILE_PATH, $output);
			  echo $output;
			}

}







if (!empty($_GET['display'])) {
		function showimage($zip_file, $file_name) {
			$z = new ZipArchive();
			if ($z->open($zip_file) !== true) {
				echo "File not found.";
				return false;
			}

			$stat = $z->statName($file_name);
			$fp   = $z->getStream($file_name);
				// search for a path/to/file matching file, returning the index of it
				$index = $z->locateName($file_name, ZipArchive::FL_NOCASE|ZipArchive::FL_NODIR);
				// get the name of the file based on the index
				$full_file_name = $z->getNameIndex($index);
				// now get the stream
				$fp = $z->getStream($full_file_name);

			if(!$fp) {
				echo "Could not load image.";
				return false;
			}

			header('Content-Type: image/jpeg');
			header('Content-Length: ' . $stat['size']);
			fpassthru($fp);
			return true;
		}

		$imgsrcencoded = $_GET['i'];
		$imagesrc = base64_decode($imgsrcencoded);
		$explodez = explode("#",$imagesrc);
		$imgg = utf8_encode($explodez[1]);
		$dirnfile = $explodez[0];
		$zipp = end((explode('/', $dirnfile)));
		$dirr = str_replace($zipp,"",$dirnfile);
		$dirr = rtrim($dirr,"/");
		$imgg = rtrim($imgg);
		chdir($dirr);
			if (empty($_GET['debug'])) {
			echo showimage($zipp,$imgg);
			}
}

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.