The task is not to cut the edges to size 900-100, and compress.
My code just cuts it off but it doesn't quite fit

public function smart_resize($file_input, $file_output, $_w, $_h, $min_w = 100, $min_h = 100) { $getimagesize = getimagesize($file_input); if (($_w==$min_w) && ($_h == $min_h)) { $width = $min_w; $height = $min_h; } else if (($getimagesize[0]>$_w) && ($getimagesize[1]>$_h)) { $width = $_w; $height = $_h; } else if (($getimagesize[0]>=$_w) && ($getimagesize[1]<$_h)) { $width = $_w; $height = ($width/($getimagesize[0]/$getimagesize[1])); } else if (($getimagesize[0]<$_w) && ($getimagesize[1]>$_h)) { $height = $_h; $width = ($height/($getimagesize[1]/$getimagesize[0])); } else { $height = $getimagesize[1]; $width = $getimagesize[0]; } if (($width<$min_w) || ($height<$min_h)) { return 'error'; } $_w = $width; $_h = $height; $w_o = $_w; $h_o = null; list($w_i, $h_i, $type) = getimagesize($file_input); $types = array('jpg','gif','jpeg','png'); $ext = $types[$type]; if ($ext) { $func = 'imagecreatefrom'.$ext; $img = $func($file_input); } if (($w_o/($w_i/$h_i))<$_h) { $h_o = $_h; $w_o = null; } if (!$h_o) { $h_o = $w_o/($w_i/$h_i); } if (!$w_o) { $w_o = $h_o/($h_i/$w_i); } $img_o = imagecreatetruecolor($w_o, $h_o); imageAlphaBlending($img_o,false); imageSaveAlpha($img_o,true); imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i); if ($h_o<$_h) { $_img_o = imagecreatetruecolor($_w, $_h); imageAlphaBlending($_img_o,false); imageSaveAlpha($_img_o,true); $src_x = (($_w/2)-($w_o/2)); $src_y = (($_h/2)-($_h/2)); imagecopy($_img_o, $img_o, 0, 0, $src_x, $src_y, $_w, $_h); imagejpeg($_img_o, ($file_output? $file_output : null), 90); imagedestroy($img_o); } else if ($h_o>=$_h) { $_img_o = imagecreatetruecolor($_w, $_h); imageAlphaBlending($_img_o,false); imageSaveAlpha($_img_o,true); $src_x = (($w_o/2)-($_w/2)); $src_y = (($h_o/2)-($_h/2)); imagecopy($_img_o, $img_o, 0, 0, $src_x, $src_y, $_w, $_h); imagejpeg($_img_o, ($file_output? $file_output : null), 90); imagedestroy($img_o); } else { imagejpeg($img_o, ($file_output? $file_output : null), 90); imagedestroy($img_o); } } 
  • Any more suggestions? - kursof

1 answer 1

Keep the old, far from ideal, but working function, which will compress and not crop images:

 // Π˜Π—ΠœΠ•ΠΠ•ΠΠ˜Π• Π ΠΠ—ΠœΠ•Π ΠžΠ’ Π˜Π—ΠžΠ‘Π ΠΠ–Π•ΠΠ˜Π― И Π—ΠΠŸΠ˜Π‘Π¬ НА Π”Π˜Π‘Πš // $image_path_original ... str ΠΏΡƒΡ‚ΡŒ ΠΊ измСняСмой ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅ // $max_width ............. максимальная ΡˆΠΈΡ€ΠΈΠ½Π° изобраТСния // $max_height ............ максимальная высотв изобраТСния // $ext ................... Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ изобраТСния (gif ΠΈΠ»ΠΈ jpg) // $image_path_save ....... ΠΏΡƒΡ‚ΡŒ, ΠΊΡƒΠ΄Π° Π±ΡƒΠ΄Π΅Ρ‚ записано ΠΈΠ·ΠΌΠ΅Π½Π΅Π½Π½ΠΎΠ΅ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ function image_resize($image_path_original, $max_width, $max_height, $ext, $image_path_save) { $max_width = intval($max_width); $max_height = intval($max_height); $size = GetImageSize($image_path_original); $width = $size[0]; $height = $size[1]; $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if ( ($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } if ($ext == "gif") { $fp = fopen($image_path_original,"rb"); $result = fread($fp,13); $this[m_signature] = mb_substr($result,0,3); $this[m_version] = mb_substr($result,3,3); $this[m_width] = ord(mb_substr($result,6,1)) + ord(mb_substr($result,7,1)) * 256; $this[m_height] = ord(mb_substr($result,8,1)) + ord(mb_substr($result,9,1)) * 256; $this[m_colorFlag] = ord(mb_substr($result,10,1)) >> 7; $this[m_background] = ord(mb_substr($result,11)); if($this[m_colorFlag]) { $tableSizeNeeded = ($this[m_background] + 1) * 3; $result = fread($fp, $tableSizeNeeded); $this[m_transparentRed] = ord(mb_substr($result,$this[m_background] * 3,1)); $this[m_transparentGreen] = ord(mb_substr($result,$this[m_background] * 3 + 1,1)); $this[m_transparentBlue] = ord(mb_substr($result,$this[m_background] * 3 + 2,1)); } fclose($fp); $src = imagecreatefromgif($image_path_original); $dst = imagecreate($tn_width, $tn_height); if($this[m_version] == '89a' && $this[m_colorFlag] == 1) { $transparent = imagecolorallocate($src, $this[m_transparentRed], $this[m_transparentGreen], $this[m_transparentBlue]); imagecolortransparent ($src, $transparent); } $transparent = imagecolorallocate($dst, 0, 0, 0); imagecolortransparent ($dst, $transparent); } else { $src = ImageCreateFromJpeg($image_path_original); $dst = imagecreatetruecolor($tn_width,$tn_height); } imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height); if ($ext == "gif") { imagegif($dst, $image_path_save); } else imagejpeg($dst, $image_path_save, 80); ImageDestroy($src); ImageDestroy($dst); } 
  • thank you very much even if you choose yours tell me what to change in your code that was similar to mine - kursof
  • There are detailed comments on the parameters of the function, I think it will not be difficult for you to simply study the ready-made solution, and the parameters of your and my functions are similar. - Miron