【发布时间】:2009-11-11 09:32:09
【问题描述】:
我想知道如何使用 getimagesize() 创建一段 PHP 代码来检查 jpg、gif、png 或任何其他类型的图像。
然后检查图像宽度是否小于以下 CSS 代码中的宽度,如果是,则 PHP 代码应在保持图像纵横比的同时重新调整图像大小,无论是横向还是纵向。
CSS 代码
overflow: hidden; width:100px; height: auto;
我发现下面列出的这段代码似乎在使用 getimagesize() 但它似乎没有重新调整图像大小。有什么办法可以解决这个问题。
<?php
function imageResize($width, $height, $target) {
//takes the larger size of the width and height and applies the
//formula accordingly...this is so this script will work
//dynamically with any size image
if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}
//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);
//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the
return "width=\"$width\" height=\"$height\"";
}
?>
<?php
//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");
?>
<!--using a standard html image tag, where you would have the
width and height, insert your new imageResize() function with
the correct attributes -->
<img src="images/sock001.jpg" <?php imageResize($mysock[0],
$mysock[1], 150); ?>>
【问题讨论】: