2014-02-27 231 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,因此高度确实发生了变化。