2013-02-19 51 views
-2

我使用的代碼,首先檢查文件夾中的文件名,然後創建網址。在url中找到它的特殊行,它是一個圖像文件。它顯示正確的圖像和地址,但如果圖像太大,需要很長時間。是否可以創建縮略圖並顯示它而不是圖像?謝謝!PHP來自url的圖片的縮略圖

require_once('simple_html_dom.php'); 
$files = scandir('files/'); 
foreach($files as $file) { 
    if($file == '.' || $file == '..') continue; 
    $file = basename($file, ".html"); 
    $url = 'http://address.com/test/'.$file; 
    $html = file_get_html($url); 
foreach($html->find('img') as $element) { 
    if (strpos($element,'address.com') !== false) { 
    $url = $element->src; 
    echo $url.'</br>'; 
    echo '<IMG SRC="',$url, '" WIDTH="128" HEIGHT="96" BORDER="0" ALT="" /><br/>'; 
    } 
    } 
} 

回答

0

你想使用CSS clip:rect(50px 218px 155px 82px);

設置寬度和高度不decress實際圖像大小,因此加載時間一樣長將仍然需要。請參閱本文一步一步div創建和css編碼。

http://www.seifi.org/css/creating-thumbnails-using-the-css-clip-property.html

另外,作爲一個方面說明,沒有什麼比實際進行TUMBNAILS!有服務端縮略圖生成器,但這是最好的,你會得到實際上做的tumbnails。

0

我在How to proportionally resize uploaded images上寫了一篇博客文章,你應該能夠從the sample I wrote調整代碼來做你想做的事。

粘貼下面的代碼,以防我的網站將來死亡。

<?php 

// *snip* Removed form stuff 
$image = imagecreatefromjpeg($pathToImage); 


// Target dimensions 
$max_width = 240; 
$max_height = 180; 


// Calculate new dimensions 
$old_width  = imagesx($image); 
$old_height  = imagesy($image); 
$scale   = min($max_width/$old_width, $max_height/$old_height); 
$new_width  = ceil($scale*$old_width); 
$new_height  = ceil($scale*$old_height); 


// Create new empty image 
$new = imagecreatetruecolor($new_width, $new_height); 


// Resample old into new 
imagecopyresampled($new, $image, 
     0, 0, 0, 0, 
     $new_width, $new_height, $old_width, $old_height); 


// Catch the image data 
ob_start(); 
imagejpeg($new, NULL, 90); 
$data = ob_get_clean(); 


// Destroy resources 
imagedestroy($image); 
imagedestroy($new); 


// Output image data 
header("Content-type: image/jpeg", true, 200); 
echo $data; 

你可能想把它放在一個函數中,並將其輸出更改爲一個文件。然後在您的foreach循環中生成縮略圖並鏈接到縮略圖,而不是原始的。你還應該檢查你是否已經爲圖像創建了一個縮略圖,所以你不要爲每個圖像多做一次。