It is necessary to convert images into php to a certain size (width, height), if they exceed it. Help.
1 answer
/** *ΠΠ΅Π»Π°Π΅Ρ ΠΏΡΠ΅Π²ΡΡΡΠΊΡ ΠΈΠ· ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠΉ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ * * @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
|