2012-01-31 38 views
3

我有一些從服務器拉出的圖像,並且$imgUrl包含圖像的路徑。如何用PHP縮小服務器端的圖像?

現在我用<img src="<?php echo $imgUrl ?>" width="100" height="200"/>或CSS來縮小圖像,但我想這樣做在PHP中,讓我的發球已經縮放圖像到DOM

任何想法?

感謝

+2

最容易使用的工具像phpthumb:http://phpthumb.sourceforge.net/ – evan 2012-01-31 18:20:31

+0

確保你做這個* *網頁會被要求先而非點播,作爲 - 使網頁執行速度變慢,從而使網站變慢。 – Incognito 2012-01-31 18:22:45

+0

@evan如果你已經回答,我會upvote。除非OP真的想寫他/她自己的圖像處理庫,我認爲這會在php中造成痛苦。 – Tim 2012-01-31 18:22:57

回答

5

該解決方案將導致當請求首次要創建的拇指。所有未來的請求都將獲取已創建的大拇指。它是利用ImageMagick

HTML:

<img src="script.php?img=example" /> 

PHP(的script.php):

$width = 140; 
$height = 80; 
$image = $_GET['img']; 
$ext = 'png'; 

// Check if file exists 
if (! file_exists('/path/to/the/'.$image.'.'.$ext)) 
{ 
    die('Unable to process the requested file.'); 
} 

// Check if a thumb already exists, otherwise create a thumb 
if (file_exists('/path/to/the/'.$image.'_thumb.'.$ext)) 
{ 
    $img = new imagick('/path/to/the/'.$image.'_thumb.'.$ext); 
} 
else 
{ 
    $img = new imagick('/path/to/the/'.$image.'.'.$ext); 
    $img->setImageFormat($ext); 
    $img->scaleImage($width, 0); 
    $img->cropImage($width, $height, 0, 0); 
    $img->writeImage('/path/to/the/'.$image.'_thumb.'.$ext); 
} 

// Return as an image 
header('Content-Type: image/'.$ext); 
echo $img; 
1

要知道,在PHP這樣做將意味着內存密集型過程或者每次訪問該圖像(如果在飛行中完成的)時間或保存圖像時(這意味着你可以使用更多的存儲空間來保存轉換後的圖像)。如果您確定這是您需要/想要的東西,那麼請使用​​進行調查。看到這個答案的想法或如何做到這一點:Image GD resize to 100px while keep the ratio

2

你應該創建一個較小的版本,並將其保存到一個文件夾。那麼你不需要在每次請求時重新設置它們(這是內存密集型的)。使用Gd或ImageMagick進行調整。

Example with GD

+0

我有很多,我的意思是很多圖像,我寧願花一些內存,然後修改現有的代碼 – Patrioticcow 2012-01-31 18:30:54

1

下面是我在用的自動取款機。隨意提取您需要:

Usage: 
<CLASS>::scale_image($dir.$name, 1024, 768, $dir.'thumb_'.$name); 

/** 
* Most simple way to get File Extension 
* @param string $path Local path to File 
* @return string Extension in lower case 
*/ 
public static function extension($path){ 
    return strtolower(pathinfo($path, PATHINFO_EXTENSION)); 
} 
/** 
* Rename Filename for more secure usage 
* @param string $name filename 
* @return string more secure filename 
*/ 
public static function secure_name($name){ 
    return urlencode(preg_replace('/[^a-z0-9 \-_\.]/i', '_', strtolower($name))); 
} 
/** 
* Scale Image without ratio changes 
* @param int $sw source width of orig image 
* @param int $sh source height of orig image 
* @param int $tw max target width 
* @param int $th max target height 
* @return array list($width, $height) 
*/ 
public static function scale($sw, $sh, $tw, $th){ 
    if ($sw > $tw && $sw/$tw > $sh/$th) { 
     $tw = $sw * ($tw/$sw); 
     $th = $sh * ($tw/$sw); 
    }else if ($sh > $th) { 
     $tw = $sw * ($th/$sh); 
     $th = $sh * ($th/$sh); 
    }else{ 
     $th = $sh; 
     $tw = $sw; 
    } 
    return array(round($tw), round($th)); 
} 
/** 
* Scale Image 
* @param string $sPath 
* @param int $width max width 
* @param int $height max height 
* @param string $tPath Optional Path for Thumbnail 
* @param int $thumbScale Scale ratio for optional Thumbnail (Width and Height/$thumbScale) 
* @return void 
*/ 
public static function scale_image($sPath, $width, $height, $tPath = NULL, $thumbScale = 10){ 
    if(!function_exists('imagecreatetruecolor')){ 
     return; 
    } 
    $ext = strtolower(self::extension($sPath)); 
    if($ext == 'jpg' or $ext == 'jpeg'){ 
     $src = imagecreatefromjpeg($sPath); 
    }elseif($ext == 'png'){ 
     $src = imagecreatefrompng($sPath); 
    }elseif($ext == 'gif'){ 
     $src = imagecreatefromgif($sPath); 
    }else{ 
     return; 
    } 
    list($sw, $sh) = getimagesize($sPath); 
    list($tw, $th) = File::scale($sw, $sh, $width, $height); 
    $trg = imagecreatetruecolor($tw, $th); 
    imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh); 
    imagejpeg($trg, $sPath, 90); 
    if($tPath){ 
     $tw = (int)$tw/$thumbScale; 
     $th = (int)$th/$thumbScale; 
     $trg = imagecreatetruecolor($tw, $th); 
     imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh); 
     imagejpeg($trg, $tPath, 90); 
    } 
} 
相關問題