可以';不要直接看到图像需要刷新


Can't see the image directly need a refresh

当我使用此函数创建带有指定链接的图像时,它不会直接显示刷新后出现

http://mysite.local/img/slide-image-321.jpg/369x360

是图像的链接

private function generate_cache($localpath, $from, $to, $width, $height) {
        $tempFrom = pathinfo($from);
        $tempTo = pathinfo($to);
        $tempPathFrom = $localpath.$tempFrom['basename'];
        $tempPathTo = $localpath.$tempTo['basename'];
        // Amazon S3
        $awsAccessKey =  Zend_Registry::getInstance()->config->amazons3->awsAccessKey;
        $awsSecretKey = Zend_Registry::getInstance()->config->amazons3->awsSecretKey;
        $bucketName = Zend_Registry::getInstance()->config->amazons3->bucketName;
        $s3 = new Zend_Service_Amazon_S3($awsAccessKey, $awsSecretKey);
        // ..
        if(file_get_contents('http://'.$bucketName.'.s3.amazonaws.com/'.$tempFrom['basename'])) {
            $content = file_get_contents('http://'.$bucketName.'.s3.amazonaws.com/'.$tempFrom['basename']);
        }
        if(!file_put_contents($tempPathFrom, $content)){
            echo "Failed to copy the file";
        }
        $resize = $this->_helper->imgResize($tempPathFrom);
        $resize->resizeImage($width, $height);
        $resize->saveImage($tempPathTo, 95);

            if(!$s3->putFile($tempPathTo, $bucketName."/".$tempTo["basename"])){
            echo "failed to put the resized image in s3";
        } else {
            // Deleting the local files
            unlink($tempPathTo);
            unlink($tempPathFrom);
            }
    }

因为在加载页面时,不会在服务器上创建图像。实际上,它可能在刷新之前创建,并在刷新后显示。

我找到了解决方案,我把最后一次上传到s3的部分移到了这里,第一次从本地加载,我将它上传到s3并删除本地,当它们刷新或第二次从s3 加载时

    if( file_exists($localpath.$tmp["basename"]) ) {
                readfile($localpath.$tmp["basename"]);
                self::upload_aws($localpath.$tmp["basename"], $bucketName."/".$tmp["basename"]);
            } elseif($s3->getObject($bucketName."/".$tmp["basename"])){
                echo $s3->getObject($bucketName."/".$tmp["basename"]);
    }

public function upload_aws(){
     if(!$s3->putFile($tempPathTo, $bucketName."/".$tempTo["basename"])){
                echo "failed to put the resized image in s3";
            } else {
                // Deleting the local files
                unlink($tempPathTo);
                unlink($tempPathFrom);
     }

}