It is necessary to convert images into php to a certain size (width, height), if they exceed it. Help.

  • Krop? resize? in proportion? - DL_
  • Proportionally rsize to do. To have a smaller replica. For example, that the width was not more than 100, if it exceeds, then resize. If does not exceed the original remains. - intranet

1 answer 1

/** *Π”Π΅Π»Π°Π΅Ρ‚ ΠΏΡ€Π΅Π²ΡŒΡŽΡˆΠΊΡƒ ΠΈΠ· ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ * * @param string $img_to - Ссли false Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ image ΠΈΠ½Π°Ρ‡Π΅ записываСт Π² Ρ„Π°ΠΉΠ» ΠΏΠΎ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΌΡƒ ΠΏΡƒΡ‚ΠΈ * @param string/image $img_from - ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ Π»ΠΈΠ±ΠΎ image * @param string $x_from, $y_from - ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π²Π΅Ρ€Ρ…Π½Π΅ΠΉ Π»Π΅Π²ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ Π²Ρ‹Ρ€Π΅Π·Π°Π΅ΠΌΠΎΠ³ΠΎ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° ΠΈΠ· источника(ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 0) * @param string $w_from - ΡˆΠΈΡ€ΠΈΠ½Π° ΠΆΠ΅Π»Π°Π΅ΠΌΠΎΠΉ ΠΏΡ€Π΅Π²ΡŒΡŽΡˆΠΊΠΈ (ΠΎΡ‚ источника), ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 160 * @param string $h_from - высота ΠΆΠ΅Π»Π°Π΅ΠΌΠΎΠΉ ΠΏΡ€Π΅Π²ΡŒΡŽΡˆΠΊΠΈ (ΠΎΡ‚ источника), Ссли false приравниваСтся ΠΊ ΡˆΠΈΡ€ΠΈΠ½Π΅. * @param string $w_to, $h_to - высота ΠΈ ΡˆΠΈΡ€ΠΈΠ½Π° ΠΏΡ€Π΅Π²ΡŒΡŽΡˆΠΊΠΈ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 160 * * @return image ΠΈΠ»ΠΈ path as string ΠΊ Π½ΠΎΠ²ΠΎΠΌΠΎΠΉ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅ */ function img_preview($img_to=false,$img_from,$x_from=0,$y_from=0,$w_from=160,$h_from=false,$w_to=160,$h_to = 160){ if(!$h_from){ $h_from = $w_from; } if(gettype($img_from) != 'resource'){ $size=getimagesize($img_from); if(getimagesize($img_from)===false){return false;} $type = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1)); $get_img = "imagecreatefrom" . $type; if (!function_exists($get_img)){ return false;} $img_from = $get_img($img_from); } $img = imagecreatetruecolor($w_to, $h_to); //Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ источник imagecopyresized ($img, $img_from,0,0,$x_from,$y_from,$w_to,$h_to, $w_from, $h_from); if($img_to){ imagejpeg($img,$img_to); return $img_to; } else{ return $img; } } 

I think it will not be difficult to call the function according to the condition about the width.

  • @intranet, if this answer helped you, you should mark it as correct by clicking on the check mark. - angry