2014-02-27 285 views
2

我使用的URL鏈接到調整大小的圖像,如:調整圖像尺寸PHP

image.php?name=butterfly&size=1100x1100 

例如:

<img src="image.php?name=butterfly&size=1100x1100"> 

我正在使用的代碼是:

<?php 
if(isset($_GET['name'])){ //name 
    $image['name'] = $_GET['name']; 
} else { 
    $image['name'] = null; 
} 
if(isset($_GET['size'])){ //dimensions 
    $image['size'] = $_GET['size']; 
    $size = explode('x', $image['size']); 
    $image['width'] = $size[0]; 
    $image['height'] = $size[1]; 
} else { 
    $image['size'] = null; 
} 
if(isset($_GET['text'])){ //text 
    $image['text'] = $_GET['text']; 
} else { 
    $image['text'] = null; 
} 
// File and new size 
$filename = 'images/'.$image["name"].'.jpeg'; 

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

// Output 
imagecreatefromjpeg($filename); 

// Set a maximum height and width 
$width = $image['width']; 
$height = $image['height']; 

// Get new dimensions 
list($width_orig, $height_orig) = getimagesize($filename); 

$ratio_orig = $width_orig/$height_orig; 

if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
} 

// Resample 
    $image_p = imagecreatetruecolor($width, $height); 
    $image = imagecreatefromjpeg($filename); 
    imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

// Output 
imagejpeg($image_p, null, 100); 
?> 

我的代碼只適用於一部分,這是寬度,圖像被調整爲它的寬度但不是高度。當我調整窗口大小時,圖片每次都會變小。謝謝你的時間,並對任何不好的解釋感到抱歉。

+0

如果圖像的大小與您的瀏覽器,然後這聽起來像一個CSS /造型問題 –

+0

我沒有任何CSS,它是一個不同的PHP代碼,當我用image.php調整窗口它不' t worj –

+1

第一個'imagecreatefromjpeg($ filename);'調用的結果不被使用。 –

回答

0

首先,問題是什麼?我沒有明白。

一個原因代碼更改僅在寬度上也可能是因爲這部分代碼的:

`if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
}` 

只有大小將會改變之一。另外,爲什麼只改變寬度的另一個原因可能是你有原始圖像和二次分辨率(例如800×600 - 比率1,34),然後你不斷改變它到更寬的一個(例如1280×720 - 比率1,77)。

希望這會有所幫助!

+0

如果你不明白這個問題,你爲什麼回答它? –

+0

沒有完全理解他的意思,我只是想幫助他,如果他提供我的答案的反饋,我可以回覆。 – Edwin

0

根據您的代碼邏輯,在任何給定時間,您的身高或寬度都會改變。 我在我的本地主機上試過了,寬度爲&我的圖片高度爲600 X 400,我通過100X100作爲參數,它將圖片大小調整爲100X66,因此高度確實發生了變化。