0

当我使用此功能创建具有指定链接的图像时,它不直接显示刷新后它出现

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

            }

    }
4

2 回答 2

0

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

于 2012-07-17T12:54:54.517 回答
0

我找到了解决方案,我将最后一次上传到 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);

     }


}
于 2012-07-18T09:04:36.067 回答