Мне было интересно, как я могу создать фрагмент кода PHP, который будет проверять изображения jpg, gif, png или любые другие типы, используя getimagesize ().
И затем проверьте, меньше ли ширина изображений, чем ширина в следующем коде 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); ?>>