2009-10-06 53 views
2

我只是想知道如何讓存儲在硬盤上的圖像的縮略圖在html頁面中使用,還需要縮略圖才能放大(如果點擊它們的原始大小)最好在同一頁上div標籤裏,我將不勝感激,如果有人可以把我在正確的方向如何用php製作縮略圖

感謝

回答

3

gd庫允許你處理圖像。 你會找到一篇文章來生成縮略圖here

如果您想允許用戶查看縮略圖和原始大小,最好的方法是保留這兩個版本。 並顯示其中一個或另一個。

0

查找PECL擴展Imagick。它通常可以使用標準軟件包管理器進行安裝。

http://se2.php.net/Imagick

您可以動態創建縮略圖和使用PHP文件(慢)爲他們提供服務或讓您存儲服務器(者優先)

5

您需要的GD縮略圖拷貝擴展啓用。下面的代碼將創建一個子目錄中的縮略圖文件名爲~tmb爲JPEG,PNG和GIF文件:

$invalid = true; 
if ($file != '.' and $file != '..') { 
    if (filetype($path_abs.$file) == "file") { 
     $ext = strtolower(substr($file,strrpos($file,'.')+1)); 
     if ($ext == 'jpg' || $ext == 'jpeg') { 
      $origimg = @imagecreatefromjpeg($path_abs.$file); 
     } elseif ($ext == 'png') { 
      $origimg = @imagecreatefrompng($path_abs.$file); 
     } elseif ($ext == 'gif') { 
      $origimg = @imagecreatefromgif($path_abs.$file); 
     } 
     if ($origimg !== false) { 
      $nheight = 0; 
      $nwidth = 0; 
      $use_orig = false; 
      if ($width<=160 and $height<160) { 
       $nwidth = $width; 
       $nheight = $height; 
       $use_orig = true; 
       $invalid = false; 
      } else { 
       if ($width>$height and $width>0) { 
        $nheight = intval((160/$width) * $height); 
        $nwidth = 160; 
       } elseif ($height>0) { 
        $nwidth = intval((160/$height) * $width); 
        $nheight = 160; 
       } else { 
        $image = false; 
       } 
       if ($nheight > 0 and $nwidth > 0) { 
        $newimg = imagecreatetruecolor($nwidth, $nheight); 
        $bgc = imagecolorallocate ($newimg, 238, 238, 238); 
        imagefilledrectangle ($newimg, 0, 0, $nwidth, $nheight, $bgc); 
        if (@imagecopyresampled($newimg, $origimg, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)) { 
         $image = imagejpeg($newimg, $path_abs.'~tmb/'.$file); 
         $invalid = false; 
        } elseif (@imagecopyresized($newimg, $origimg, 0, 0, 0, 0, $nwidth, $nheight, $width, $height)) { 
         $image = imagejpeg($newimg, $path_abs.'~tmb/'.$file); 
         $invalid = false; 
        } 
       } 
      } 
     } 
    } 
} 
if (!$invalid) { 
    if ($use_orig) { 
     echo '<img src="'.$file.'" alt="" />'; 
    } else { 
     echo '<img src="~tmb/'.$file.'" alt="" />'; 
    } 
} else { 
    echo '<p>Error for file '.$file.'</p>'; 
} 

在上面的代碼中,他們調整大小以160x160的,但保持縱橫比。

+0

請注意背景顏色('$ bgc'):爲了我的目的,我需要它來匹配網站樣式。您還應該注意提供的文件字符串,以確保腳本無法退出圖像路徑。 – 2009-10-06 13:30:50

+0

你最好使用pathinfo來檢查擴展名。 – 2011-10-13 10:27:38

0

我發現的最好方法是使用phpThumb類(http://phpthumb.sourceforge.net/)。

它擁有您所需要的一切,包括緩存,過濾器,水印和其他很酷的東西。看看演示頁面。

0

maxImageUpload用於創建縮略圖,正常圖像與原始圖像。

您可以使用jQuery來放大縮略圖。

+0

你爲什麼要放大縮略圖?拇指看起來像垃圾 – 2011-01-09 11:14:47

+0

放大的圖像不是縮略圖而是原始圖像。我更喜歡使用jQuery來擴大選項。 – Paulraj 2011-01-10 10:16:40

1

它的那麼簡單,如果你有[email protected]

$ffmpeg = "ffmpeg Installed path" 

$flvfile = "source video file with root path" 

$png_path " "Destination video file with root path and file type" 

exec("$ffmpeg -y -i $flvfile -vframes 1 -ss 00:01:60 -an -vcodec png -f rawvideo -s 110x90 $png_path"); 

任何疑問郵件的所有最好的....