この機能を完了しようとしていますが、1つの小さな問題を除いてはうまく動作しているように見えます。画像を左に大きく移動し、 。サイズ変更された画像に黒い四角が入っています
私がしようとしているのは、指定された$ thumb_wにイメージのサイズを変更する機能です。サイズが変更された後に$ thumb_hより大きくなった場合、底部が切り抜かれます。
HERESに私の機能コード:
function resize_upload ($tmp, $thumb_w, $thumb_h, $img_name, $img_ext, $img_path)
{
if ($img_ext == 'jpg' || $img_ext == 'jpeg' || $img_ext == 'png' || $img_ext == 'gif')
{
if ($img_ext == 'jpg' || $img_ext == 'jpeg')
$source_img = imagecreatefromjpeg($tmp);
else if ($img_ext=='png')
$source_img = imagecreatefrompng($tmp);
else
$source_img = imagecreatefromgif($tmp);
$orig_w = imagesx($source_img);
$orig_h = imagesy($source_img);
$w_ratio = ($thumb_w/$orig_w);
$h_ratio = ($thumb_h/$orig_h);
if ($orig_w > $orig_h)
{
$crop_w = round($orig_w * $h_ratio);
$crop_h = $thumb_h;
$src_x = ceil(($orig_w - $thumb_w)/2);
$src_y = 0;
}
elseif ($orig_w < $orig_h)
{
$crop_h = round($orig_h * $w_ratio);
$crop_w = $thumb_w;
$src_x = 0;
$src_y = ceil(($orig_h - $thumb_h)/2);
}
else
{
$crop_w = $thumb_w;
$crop_h = $thumb_h;
$src_x = 0;
$src_y = 0;
}
$thumb_img = imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($thumb_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
imagejpeg($thumb_img, $img_path.'/'.$img_name.'.'.$img_ext, 100);
imagedestroy($thumb_img);
imagedestroy($source_img);
}
}
はHERESに私は関数を呼び出す方法:
resize_upload ($_FILES['image_main']['tmp_name'], 556, 346, $img_name, $img_ext, '../wp-content/themes/my-theme/images/projects');
HERESにどのようなイメージが機能した後のように見える終わると、そのことを行います。
右側の黒を参照してください。おそらく私は理解できないいくつかの数学の問題です。どんな助けでも大歓迎です。
ありがとうございました。私は事を説明してくれたことを嬉しく思ったので、私は実際に正しいコードを与えるよりも、実際にそれらについて考える必要がありました。 – scarhand