S
S
SimonPomidorkin2018-04-29 18:06:00
PHP
SimonPomidorkin, 2018-04-29 18:06:00

Resizing an image with php?

Hello, this may seem like a stupid question, but since I'm still a beginner, I don't understand what I'm doing wrong, so I'm asking for help.
The essence of the problem is this: For example, the image exceeds the dimensions and I need to bring it to the maximum allowable dimensions, which are written in the $width and $height variables .
If the image is oversized, but it is horizontal, everything works as intended. If the image is vertical, then it is not reduced to the maximum allowed value, which is stored in the variable height . What am I doing wrong? Maybe there is some more elegant way to bring the image to the maximum allowed dimensions?
PS I need php to do this, and js

The code
<?php
//код приводит изображение к максимально заданным размерам
header('Content-Type: image/jpeg');
$filename = 'imagev.jpg';
$width = 300; //maximum values
$height = 300;
list($width_orig, $height_orig) = getimagesize($filename);// присваеваем значения переменным ширины и высоты оригинала
if ($width_orig > $width) {
$w_ratio = $width / $width_orig;
$fin_hegight = $height_orig * $w_ratio;
$fin_width = $width;
} elseif ($height_orig > $height){
$h_ratio = $height / $height_orig;
$fin_width = $width_orig * $h_ratio;
$fin_height = $height;
}
$image_p = imagecreatetruecolor($fin_width, $fin_hegight);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $fin_width, $fin_hegight, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);
?>

Code: https://pastebin.com/FL76yu24

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-29
@Yan-s

image.intervention.io/api/fit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question