0

私は .JPG 画像のサイズを完全に変更するスクリプトを持っています。ただし、.PNG en .GIF 画像も追加するオプションをユーザーに提供したいと考えています。これらの形式もサポートするように、次のスクリプトを拡張するのを手伝ってもらえますか? 前もって感謝します!

$image = $_POST['bedrijfslogo'];
$new_image = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";
copy($_POST['bedrijfslogo'],"copylogo.jpg");
$width=250; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($image);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($image);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin, $new_image);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
unlink($image);
4

3 に答える 3

0

私のコメントに続いて、によって返されたデータから画像のタイプを抽出できることに気付きましたgetimagesize()

これを試してください。PHPインスタンスが理解できる(テストされていない)あらゆるタイプの画像を受け入れる必要があります

// Get file paths
// Taking a local file path direct from the user? This is a *big* security risk...
$srcPath = $_POST['bedrijfslogo'];
$dstPath = "../subdomains/{$gemeente7}/httpdocs/{$plaatsnaam7}/{$bedrijfsnaam7}/bedrijfslogo.jpg";

// Not really sure why this is here, you don't seem to use the copy of the file
copy($srcPath, "copylogo.jpg");

// Get information about source image
$info = getimagesize($srcPath);
$srcWidth = $info[0];
$srcHeight = $info[1];
$srcType = $info[2];

// Calculate new width and height
// Width is fixed, height calculated from width
$dstWidth = 250;
$dstHeight = round($dstWidth * $srcHeight / $srcWidth);

// Get the image types supported by this instance of PHP
$supportedTypes = imagetypes();

// Create the source image resource based on it's type
switch ($srcType) {
  case IMG_GIF:
    if ($supportedTypes & IMG_GIF) {
      $srcImage = imagecreatefromgif($srcPath);
    } else {
      // GIF support not enabled on your PHP instance. Handle error here.
      exit('GIF support not enabled on this PHP instance');
    }
    break;
  case IMG_JPG: case IMG_JPEG: // Think these might have the same value?
    if ($supportedTypes & IMG_JPG) {
      $srcImage = imagecreatefromjpeg($srcPath);
    } else {
      // JPEG support not enabled on your PHP instance. Handle error here.
      exit('JPEG support not enabled on this PHP instance');
    }
    break;
  case IMG_PNG:
    if ($supportedTypes & IMG_PNG) {
      $srcImage = imagecreatefrompng($srcPath);
    } else {
      // PNG support not enabled on your PHP instance. Handle error here.
      exit('PNG support not enabled on this PHP instance');
    }
    break;
  case IMG_WBMP:
    if ($supportedTypes & IMG_WBMP) {
      $srcImage = imagecreatefromwbmp($srcPath);
    } else {
      // WBMP support not enabled on your PHP instance. Handle error here.
      exit('WBMP support not enabled on this PHP instance');
    }
    break;
  case IMG_XPM:
    if ($supportedTypes & IMG_XPM) {
      $srcImage = imagecreatefromxpm($srcPath);
    } else {
      // XPM support not enabled on your PHP instance. Handle error here.
      exit('XPM support not enabled on this PHP instance');
    }
    break;
  default:
    // Unknown input file type. Handle error here. Currently prints some debugging info
    echo 'Unknown input file type';
    var_dump($info);
    exit;
}

// Create the destination image resource
$dstImage = imagecreatetruecolor($dstWidth, $dstHeight);

// Resample source image onto destination image
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $dstWidth + 1, $dstHeight + 1, $srcWidth, $srcHeight);

// Save new image to disk
imagejpeg($dstImage, $dstPath);

// Destroy resources
imagedestroy($srcImage);
imagedestroy($dstImage);
$srcImage = $dstImage = NULL;

// Remove source image - are you sure you want to do this?
unlink($srcPath);

現在、このコードはローカルソースイメージで機能するように設計されているようですが、使用方法は、ユーザーからURLを取得してリモートイメージをダウンロードしている可能性があることを示しています。その場合はお知らせください。目的に合わせて少し変更します。

于 2012-08-10T12:37:54.023 に答える
0

変化とは、

$images_orig = ImageCreateFromJPEG($image); //for jpg

ここでドキュメントを確認できます

また、ImageMagick と GDおよびPHPのいくつかの比較を行うこともできます: ImageMagick - Manual 。

お役に立てれば

于 2012-08-12T13:55:33.310 に答える
0

必要な出力ファイルのタイプに応じて、以下を追加して選択します。

imagecreatefrompng 

 imagecreatefromgif 

 imagepng 

 imagegif
于 2012-08-10T12:20:40.193 に答える