2011-07-18 10 views
1

画像処理にImageMagickを使用するいくつかのPHPコードを変換したいと思います。私はGDの使用に関しては初心者ですが、いくつかの指示やコードの提案を得ることができれば幸いです。ImageMagickコードをGD(php)に変換

現在のPHPコードは、それを見たため

$rand = rand(); 
$galleryWidth ='245'; 
$galleryHeight ='245'; 

$result = array(); 

if (isset($_FILES['photoupload'])) 
{ 
    $file = $_FILES['photoupload']['tmp_name']; 
    $error = false; 
    $size = false; 



     list($file_name, $file_type) = split('[.]', $_FILES["photoupload"]["name"]); 

     move_uploaded_file($_FILES["photoupload"]["tmp_name"], 
     "./photos/org/".$rand.'.'.$file_type); 

     list($width,$height)=getimagesize('./photos/org/'. $rand.'.'.$file_type); 


     if(($galleryWidth/$width) < ($galleryHeight/$height)){ 
     exec("C:/imagemagick/convert ./photos/org/". $rand.".".$file_type."\ 
      -thumbnail ".round(($width*($galleryWidth/$width)), 0)."x".round(($height*($galleryWidth/$width)), 0)." \ 
      -quality 90 ./photos/".$_GET['id'].".jpg"); 
     } 
     else{ 
     exec("C:/imagemagick/convert ./photos/org/". $rand.".".$file_type."\ 
      -thumbnail ".round(($width*($galleryHeight/$height)), 0)."x".round(($height*($galleryHeight/$height)), 0)." \ 
      -quality 90 ./photos/".$_GET['id'].".jpg"); 
     } 
     $result['result'] = 'success'; 
     $result['size'] = "Uploaded an image ({$size['mime']}) with {$size[0]}px/{$size[1]}px."; 

} 
?> 

おかげ下に見ることができます!

+0

普遍的な「変換」技術はありません。達成しようとしていることを教えてください。それをあなたのコードから理解するのは難しいです。 – Hnatt

+0

すみません。基本的には、画像をアップロードするためのPHPコードです。サイズ変更や画質の低下を防ぐにはImageMagickが必要です。 "-thumbnail"と "-quality"はPHPコードではなく、ImageMagickコードです。 GDは似た機能を持っていますが、構文は違っています。最も重要なことは、サムネイルが正しい比率で作成されることです。 – jman

+0

GDに戻したい理由はありますか?この 'exec()'ハッカーをする代わりに[Imagick native php extension](http://www.php.net/manual/en/book.imagick.php)に切り替えることをお勧めします。あなたは同じ機能を持っていますが、コードはクロスプラットフォーム&クリーナーになります。 – CuriousMind

答えて

2

GDsファイル形式のサポートは、ImageMagickに比べて少し制限がありますが、次のようなものを探しています。

$inputPath = "./photos/org/{$rand}.{$file_type}"; 
$outputPath = "./photos/{$imageId}.jpg"; 

list($old_width, $old_height) = getimagesize($inputPath); 

// -- Calculate the new_width and new_height here, however you want to. 
$new_width = 250; 
$new_height = 250; 

// -- Initialise the source image container 
if($file_type == 'png') 
    $src_img = imagecreatefrompng($inputPath); 
else if($file_type == 'jpeg' || $file_type == 'jpg') 
    $src_img = imagecreatefromjpeg($inputPath); 
else 
    throw new Exception("Unsupported file format."); 

// -- Prepare the new image container 
$dst_img = ImageCreateTrueColor($new_width, $new_height); 

// -- Resample the "old" image to the "new" image 
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 

// -- Save the new canvas (the 90 represents the quality percentage) 
imagejpeg($dst_img, $outputPath, 90); 

// -- Perform cleanup on image containers. 
imagedestroy($dst_img); 
imagedestroy($src_img); 
+0

うん@nctrnl、あなたのImageMagickコールはイメージを小さなサイズにスケーリングしているだけなので、 'imagecopyresampled'は確かに将来的にもっと複雑な画像サムネイル要求がある場合は、[TimThumb PHPライブラリ](http://www.binarymoon.co.uk/projects/timthumb/)を参照してください。 –

+0

エラー:getimagesize(./ photos/org/1713。)[function.getimagesize]:ストリームを開けませんでした:C:\ xxx \ xxx \ xxx \ xxxにそのようなファイルやディレクトリがありません\ xxx \ employees \ upload_foto.php 6行目 致命的なエラー:C:\ xxx \ xxx \ xxx \ xxx \ xxx \ employees \ upload_foto.phpに「Unsupported file format。スタックトレース:#0 {メイン}がC:\ xxx \ xxx \ xxx \ xxx \ xxx \ employees \ upload_foto.php 18行目 – jman

+0

@nctrnlあなたの '$ inputPath'が画像のパスに設定されていないので、それを聞いて、それを修正してください。このエラーが消えるのを見てください。 –

関連する問題