resize image before caching
Results 1 to 3 of 3

Thread: resize image before caching

  1. #1
    Member
    Join Date
    May 2007
    Posts
    46

    resize image before caching

    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() but the image is still not resized.

    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 :

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

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    1. How do you know the image is never resized? You resize it and store it using the image resource in $new_image, yet you never do anything with that resource afterwards (such as saving it to a file).

    2. Where do you ever define $output?

  3. #3
    Member
    Join Date
    May 2007
    Posts
    46
    then what do i have to do?

    i tried replacing the imagejpeg() line by this:
    imagepng($new_image, $CACHE_FILE_PATH);
    It does save a 30x30 image but the image is all black instead of my real image...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •