2012-07-17 36 views
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); 

      } 

    } 

回答

0

因爲當您的頁面加載時,圖片將不會在服務器上創建。實際上,它可能會在刷新之前創建並在刷新後顯示。

+0

(不Amazon S3的)其工作正常,問題出在S3中,我不知道如何解決它 \t私有函數generate_cache($來自$到,$寬度, $ height){ \t \t $ resize = $ this - > _ helper-> imgResize($ from); \t \t $ resize-> resizeImage($ width,$ height); \t \t $ resize-> saveImage($ to,95); } – 2012-07-17 13:31:20

+0

不知道我向你展示了正確的方法,但是在嘗試s3時,可能需要一些時間來處理圖像,如果它在服務器本身上,圖像將被瞬間處理。 – Jithin 2012-07-18 05:53:42

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

    } 


} 
當我只有服務器工作
相關問題