質問

したいユーザーがアップロードのアバタータイプの画像を様々な方式(GIF、JPEG及びPNGの少なくともで保存してい PNGデータベースの用途にでも便.の場合は画像の大型化,pixelwiseいリサイズして、DB-挿入出来ます。

には、どうするのがベストな使GDいサイズ変更されているとPNGください。)

編集:悲しみ GD のサーバで利用、 ImageMagick.

役に立ちましたか?

解決

<?php                                              
/*
Resizes an image and converts it to PNG returning the PNG data as a string
*/
function imageToPng($srcFile, $maxSize = 100) {  
    list($width_orig, $height_orig, $type) = getimagesize($srcFile);        

    // Get the aspect ratio
    $ratio_orig = $width_orig / $height_orig;

    $width  = $maxSize; 
    $height = $maxSize;

    // resize to height (orig is portrait) 
    if ($ratio_orig < 1) {
        $width = $height * $ratio_orig;
    } 
    // resize to width (orig is landscape)
    else {
        $height = $width / $ratio_orig;
    }

    // Temporarily increase the memory limit to allow for larger images
    ini_set('memory_limit', '32M'); 

    switch ($type) 
    {
        case IMAGETYPE_GIF: 
            $image = imagecreatefromgif($srcFile); 
            break;   
        case IMAGETYPE_JPEG:  
            $image = imagecreatefromjpeg($srcFile); 
            break;   
        case IMAGETYPE_PNG:  
            $image = imagecreatefrompng($srcFile);
            break; 
        default:
            throw new Exception('Unrecognized image type ' . $type);
    }

    // create a new blank image
    $newImage = imagecreatetruecolor($width, $height);

    // Copy the old image to the new image
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output to a temp file
    $destFile = tempnam();
    imagepng($newImage, $destFile);  

    // Free memory                           
    imagedestroy($newImage);

    if ( is_file($destFile) ) {
        $f = fopen($destFile, 'rb');   
        $data = fread($f);       
        fclose($f);

        // Remove the tempfile
        unlink($destFile);    
        return $data;
    }

    throw new Exception('Image conversion failed.');
}

他のヒント

プロセスの手順はこのようになっているでしょう:

  1. 確認filetype
  2. 負荷の場合、イメージでサポートされたfiletype入GD用 imagecreatefrom*
  3. リサイズを使用 imagecopyresize または imagecopyresampled
  4. 保存画像を用い imagepng($取り扱い'ファイル名を指定します。png',$質$ル)

ImageMagickがより速く、より良い画像が設定可能な、最後には(IMO)により,簡単なコードです。

@ceejayozるのを待つだけで新GDでOOP MySQLiのように、かなり悪い:)

を利用する場合はgdlib、利用gdlib2以上とします。この機能と呼ばれるものimagecopyresampled()には、補間画素をサイズ変更ともかなり良くなったと思います。

また、私は周りの当期純ることで画像を保存するデータベースが悪い形式:

  • で遅めのクリネックススタジアム、ディスク
  • サーバーを実行スクリプトのイメージにな 単にサービスを提供するファイル
  • スクリプトに今を担う多くのものをwebサーバの使用 取扱い:
    • 設定に適切なコンテンツタイプヘッダー
    • 設定に適切なキャッシュ/タイムアウト/E-タグのヘッダ、お客様が適切なキャッシュすることができます。がないことが適切でないと、画像ィスクリプトしまう可能性があることが望のサーバーに負荷をかけるものです。

いずみさの、私はいままでし続ける必要がなデータベースや画像ファイルを同期させます。私がお勧めする技術を開発しているのですが。

ご確認くださいませImageMagickサーバ?

私はゲストを使用ョンからの質問は、タグを付けサポートに対するコミットメント.ホスティング会社を使っていないImageMagick拡張子をonにphpinfo().

が聞いてわ は以下のようになります。ImageMagickプログラムからのPHPコード.るだけではありませんIMインターフェースPHP出すことはできなかったがコIMプログラムから直接クリアしました。

げていきたいと思いますが同じオプションです。

I 強く 同意--保存の画像データベースは良い方法がありました。

何か、このようなもの:


<?php
   //Input file
   $file = "myImage.png";
   $img = ImageCreateFromPNG($file);

   //Dimensions
   $width = imagesx($img);
   $height = imagesy($img);
   $max_width = 300;
   $max_height = 300;
   $percentage = 1;

   //Image scaling calculations
   if ( $width > $max_width ) { 
      $percentage = ($height / ($width / $max_width)) > $max_height ?
           $height / $max_height :
           $width / $max_width;
   }
   elseif ( $height > $max_height) {
      $percentage = ($width / ($height / $max_height)) > $max_width ? 
           $width / $max_width :
           $height / $max_height;
   }
   $new_width = $width / $percentage;
   $new_height = $height / $percentage;

   //scaled image
   $out = imagecreatetruecolor($new_width, $new_height);
   imagecopyresampled($out, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

   //output image
   imagepng($out);
?>

しんのカテコードであ構文エラーがでお渡し適正に表示す方法ですということになります。また、想定してPNGファイルです。するためには何らかのスイッチ決定にファイルタイプです。

この記事 が感じられるように思えます。必要な変更の保存imagejpeg()関数をimagepng()で保存するファイルを文字列ではなく出力することができます。ページ以外はすべきであると簡単にコピー/貼り付ける既存のコードです。

はGD絶対に必要ですか? ImageMagick より速く、より良い画像が設定可能な、最後には(IMO)により,簡単なコードです。

と思い このページ 良い点です。このimagecreatefrom(jpeg/gif/png)のサイズの変更は、変換、イメージその成果の可視性を高めることはブラウザです。代わりに出力のブラウザまでに出力されるBLOB、DBなく多くのminuttes ースコードの書き換え.

phpThumb 高レベルの抽象化されるに値します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top