2011-03-22 38 views
0

我打算在我的網站中生成RSS訂閱源...爲了在RSS訂閱源中顯示圖像,我將它們從源系統(與服務器不同)中取出。這只是爲了減少我的服務器中的負載和帶寬使用。如何重新調整圖像的大小

由於圖像尺寸相當大,因此需要在運行時調整圖像大小。

請指導如何可以做到這一點

+0

不要這樣做。將圖像提取到服務器上,在那裏調整大小,並將它們存儲爲靜態資源。調整大小非常昂貴 – 2011-03-22 11:39:18

+0

他可以隨時在上傳時調整大小。 – 2011-03-22 12:52:10

回答

2
  • 這是我打的是同時使用的功能,它被設計成自動維持調整後的圖像的約束比例

用法

imageResize('old_image.jpg', 200, 'new_image.jpg'); 

function imageResize($image, $thumb_width, $new_filename) 
{ 
    $max_width = $thumb_width; 
    //Check if GD extension is loaded 
    if (!extension_loaded('gd') && !extension_loaded('gd2')) { 
    trigger_error("GD is not loaded", E_USER_WARNING); 
    return false; 
    } 
    //Get Image size info 
    list($width_orig, $height_orig, $image_type) = getimagesize($image); 
    switch ($image_type) { 
    case 1: 
     $im = imagecreatefromgif($image); 
     break; 
    case 2: 
     $im = imagecreatefromjpeg($image); 
     break; 
    case 3: 
     $im = imagecreatefrompng($image); 
     break; 
    default: 
     trigger_error('Unsupported filetype!', E_USER_WARNING); 
     break; 
    } 
    //calculate the aspect ratio 
    $aspect_ratio = (float) $height_orig/$width_orig; 
    //calulate the thumbnail width based on the height 
    $thumb_height = round($thumb_width * $aspect_ratio); 
    while ($thumb_height > $max_width) { 
    $thumb_width -= 10; 
    $thumb_height = round($thumb_width * $aspect_ratio); 
    } 
    $new_image = imagecreatetruecolor($thumb_width, $thumb_height); 
    //Check if this image is PNG or GIF, then set if Transparent 
    if (($image_type == 1) OR ($image_type == 3)) { 
    imagealphablending($new_image, false); 
    imagesavealpha($new_image, true); 
    $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); 
    imagefilledrectangle($new_image, 0, 0, $thumb_width, $thumb_height, $transparent); 
    } 
    imagecopyresampled($new_image, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig); 
    //Generate the file, and rename it to $new_filename 
    switch ($image_type) { 
    case 1: 
     imagegif($new_image, $new_filename); 
     break; 
    case 2: 
     imagejpeg($new_image, $new_filename); 
     break; 
    case 3: 
     imagepng($new_image, $new_filename); 
     break; 
    default: 
     trigger_error('Failed resize image!', E_USER_WARNING); 
     break; 
    } 
    return $new_filename; 
} 
2

答案很簡單:

不上飛調整圖像。永遠不要這樣做。

不像著名的回波對打印或單與雙引號的問題,調整圖像大小確實真正嚴重損害系統性能。所以,最終會出現RSS饋送出現故障並暫停服務器

1

請注意,您不應該在飛行中使用縮略圖圖像製作RSS源。而是將生成的訂閱源(包含圖像)保存在.rss文件中並提供。

現在,當您添加新項目時,您將更新.rss文件。

1

我用這個:

<Directory {DOCUMENT_ROOT}/tn/> 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule \.png$ /tn/create.php [PT,NE,L] 
</Directory> 

背後nginx的偉大工程。

在編寫「create.php」之前要非常小心地確保您瞭解PHP。這將很容易造成一個巨大的安全漏洞。

注意:如果您有大量不能輕易靜態生成的圖像,此解決方案是值得的。

相關問題