Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

resize image before caching

$
0
0

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://www.pirate-punk.com/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;
			}

Viewing all articles
Browse latest Browse all 13200

Trending Articles