0

正常に機能する既存の画像アップロードスクリプト(下記)がありますが、それにトリミング機能を追加したいので、アップロードされた各写真は正しいアスペクト比を維持しますが、たとえば200x200pxにトリミングされます。

これに関連するSOに関する他の質問を見てきましたが、理想的には、まったく新しいものを実装するのではなく、スクリプトにトリミングを追加したいと思います。

誰か助けてもらえますか?

いつもありがとう。

mkdir("images/$user_id");
$saveto = "images/$user_id/$user_id.jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
$typeok = TRUE;

switch($_FILES['image']['type'])
{
    case "image/gif":   $src = imagecreatefromgif($saveto); break;

    case "image/jpeg":  // Both regular and progressive jpegs
    case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;

    case "image/png":   $src = imagecreatefrompng($saveto); break;

    default:            $typeok = FALSE; break;
}

if ($typeok)
{




    list($w, $h) = getimagesize($saveto);
    $max = 200;
    $tw  = $w;
    $th  = $h;

    if ($w > $h && $max < $w)
    {
        $th = $max / $w * $h;
        $tw = $max;
    }
    elseif ($h > $w && $max < $h)
    {
        $tw = $max / $h * $w;
        $th = $max;
    }
    elseif ($max < $w)
    {
        $tw = $th = $max;
    }

    $tmp = imagecreatetruecolor($tw, $th);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
    imageconvolution($tmp, array( // Sharpen image
                            array(-1, -1, -1),
                            array(-1, 16, -1),
                            array(-1, -1, -1)
                           ), 8, 0);
    imagejpeg($tmp, $saveto);
    imagedestroy($tmp);
    imagedestroy($src);


}

編集:私はそれがそれ自身のページにあるときにうまくいく次のスクリプトを見つけました、しかし私はそれを私の既存のアップロードスクリプトの中または後に実装するのに問題があります-私はいくつかの'ストリームを開くことができませんでした:そのようなファイルまたはディレクトリはありません'エラー-ただし、画像へのパスは正しいです(確かにエコーアウトしました):

$filename = 'images/$user_id/$user_id.jpg';

// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);

// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 25;
$top = 25;

// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = 200;

// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename, 100);

誰かが私が2つをまとめるのを手伝ってもらえますか?

ありがとう

4

2 に答える 2

1

見てください:Gregwar / Image

非常に使いやすく、非常に効率的です。

  • resize($ width、$ height、$ background):画像のサイズを変更し、縮尺を維持し、拡大しない

  • scaleResize($ width、$ height、$ background):画像のサイズを変更し、スケールを保持します

  • forceResize($ width、$ height、$ background):画像のサイズを変更し、画像を正確に$width×$heightに調整します

  • CropResize($ width、$ height、$ background):画像のサイズを変更し、縮尺を維持して空白をトリミングします

于 2012-09-16T16:27:42.210 に答える
0

回答してくれた皆さんに感謝します-スクリプトを変更して動作させました

$filename = 'images/$user_id/$user_id.jpg';

$filename = "images/$user_id/$user_id.jpg";
于 2012-09-16T17:04:17.767 に答える