2015-04-26 102 views
-1

我用下面的代碼獲取圖像:如何向上縮放圖像{} PHP

$url = "http://maps.googleapis.com/maps/api/staticmap?size=800x600&zoom=12&center=(10.763705518561297,106.64444580078126)"; 
$content = file_get_contents($url); 

我收到一個640像素X 640像素的圖像。同時我期望的800像素X 600像素之一。如何將該圖像縮放到800像素x 600像素?

+1

參見[這裏在谷歌(https://www.google.com/search?q=resize+images+php&gws_rd=ssl) –

+0

首先,什麼是'的file_get_contents()'有這樣做?這就像問「在雨天下午我怎麼做?」。然後,如果您打算將此服務提供給網頁瀏覽器,則應使瀏覽器相應地縮放圖像,因爲這樣只會耗費CPU時間,增加傳輸的數據,並且很可能會發生瀏覽器必須縮放圖像爲了適應它運行的任何顯示。實際上,如果可能的話,告訴瀏覽器在Google上找到圖片的位置,這樣它就不會先繞過你的網絡服務器。 –

+0

這實際上並不是一個圖像,是嗎,這是Google Map框,是的?在這種情況下,PHP預渲染處理(如Gauravs答案中給出的)將不起作用。 – Martin

回答

0

嘗試這樣做:

<?php 
// File and new size 
$filename = 'test.jpg'; 
$percent = 0.5; 

// Content type 
header('Content-Type: image/jpeg'); 

// Get new sizes 
list($width, $height) = getimagesize($filename); 
$newwidth = $width * $percent; 
$newheight = $height * $percent; 

// Load 
$thumb = imagecreatetruecolor($newwidth, $newheight); 
$source = imagecreatefromjpeg($filename); 

// Resize 
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

// Output 
imagejpeg($thumb); 
?> 

這裏,重新調整了50%完成,你可以改變參數按您的要求。您甚至可以使用Imagick進行圖像處理。看,如果有幫助。

簡稱:PHP Manual