php:在缓存之前调整图像大小

时间:2013-07-26 05:24:19

标签: php image caching png jpeg

此脚本将加载图像,然后保存本地副本以进行缓存

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

如何在保存之前将图像大小调整为300x300px?

2 个答案:

答案 0 :(得分:1)

我认为这个链接可以帮到你,(imagecopyresampled php的功能)

http://php.net/manual/en/function.imagecopyresampled.php

答案 1 :(得分:0)

你需要使用

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

检查此网址image resize。希望它是完整的帮助