It is necessary to reduce the image proportionally, but, specifying only its width, and the height to be determined automatically, based on the size of the width. Is it possible?

$src = imagecreatefrompng($iii); $img = getimagesize($iii); $width = $img[0]; $height = $img[1]; $resize_width = ($width*50)/100; $resize_height = ($height*50)/100; $new = ImageCreateTrueColor($resize_width,$resize_height); imagealphablending($new, false); imagesavealpha($new, true); ImageCopyResampled($new, $src, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height); imagepng($new, './img'.p($_POST['title']).'.png', 9, PNG_ALL_FILTERS); imagedestroy(array($img,$src,$new)); 

ImageCopyResampled($new, $src, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height); for example, you can specify not $resize_height but auto but not so)

  • And what does my code do in your question? - Visman
  • To see what I am doing wrong, just as it is not right, I do it) - emtecif
  • Try using this library here github.com/claviska/SimpleImage - Adobe
  • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - Cerbo

1 answer 1

I use such calculations

 $size = getimagesize($file); // берем размеры картинки из файла $xr = ($width == 0) ? 1 : $width / $size[0]; // тут $width нужная ширина, 0 - не учитывать (расчет автоматом из нужно высоты) $yr = ($height == 0) ? 1 : $height / $size[1]; // тут $height нужная высота, 0 - не учитывать (расчет автоматом из нужно ширины) $r = min($xr, $yr, 1); $width = round($size[0] * $r); // $width - ширина на выходе $height = round($size[1] * $r); // $height - высота на выходе 

PS Yes. If both the width and height at the input are set, then the new dimensions of the image are calculated while maintaining the proportions from the old dimensions, but so that the new dimensions fit into the required width / height.

  • At the output I get the number 300, the original size of the picture, what did I do wrong? - emtecif
  • @emtecif, so it is not necessary to replace variables with numbers, and even wrong. - Visman
  • Thanks, works - emtecif