Image conversion.

You need to convert images in php to a certain size (width, height), if they exceed it. Help.


Answer 1, authority 100%

/**
     *    
     *
     * @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 calling a function based on a width condition won’t be hard.