2016-06-20 1 views
0

私はPHP初心者ですが、まだその言語を把握しようとしています。 私はPHPを使って正方形にアップロードしている画像をトリミングする必要があります。ここでは(正常に動作している)画像をアップロードするために私の現在のコードは次のとおりです。サーバにアップロードするときにphpを使って正方形に画像を切り抜く

<?php 


error_reporting(0); 



    $sUploadDirectory = 'uploads/'; 

    $aFileTypesAllowed = array('image/jpeg', 'image/gif', 'image/png'); 
    $aExtensionsAllowed = array('gif', 'png', 'jpg', 'jpeg'); 


    $aJSExtensions  = 'new Array("gif", "png", "jpg", "jpeg")'; 


    $bImagesOnly = true; 

    $iMaxFileSize = 102400; 




if(isset($_REQUEST['upload']) && $_REQUEST['upload'] == 'true') { 


    $bSuccess = true; 
    $sErrorMsg = 'Your image was successfully uploaded.'; 


    if (array_search($_FILES['myFile']['type'], $aFileTypesAllowed) === false || 
     !in_array(end(explode('.', strtolower($_FILES['myFile']['name']))), $aExtensionsAllowed)) { 

     $bSuccess = false; 
     $sErrorMsg = 'Invalid file format. Acceptable formats are: ' . implode(', ', $aFileTypesAllowed); 


    } else if ($bImagesOnly && !(getimagesize($_FILES['myFile']['tmp_name']))) { 

     $bSuccess = false; 
     $sErrorMsg = 'The image is invalid or corrupt. Please select another.'; 


    } else if ($_FILES['myFile']['size'] > $iMaxFileSize) { 

     $bSuccess = false; 
     $sErrorMsg = 'The file size of your property photo must not exceed ' . ($iMaxFileSize/1024) . 'Kb. Please try again.'; 


    } else { 



     if ([email protected]_uploaded_file($_FILES['myFile']['tmp_name'], $sUploadDirectory . $_FILES['myFile']['name'])) { 
      $bSuccess = false; 
      $sErrorMsg = 'An unexpected error occurred while saving your uploaded photo. Please try again.'; 
     } 

    } 




    print '<html>' . 
       '<head>' . 
        '<script type="text/javascript">' . 
         'parent.uploadResult(' . ($bSuccess ? 'true' : 'false') . ', \'' . $sErrorMsg . '\', \'' . $sUploadDirectory . $_FILES['myFile']['name'] . '\');' . 
        '</script>' . 
       '</head>' . 
       '<body></body>' . 
      '</html>'; 


    die(); 

} 
?> 

私はそれはデスクトップ上で動作、ファイルサイズを確認し、最後の引数の後にこれを追加しようとしました、しかしモバイルでありません

//********************* 
//crop image into sqaure 
//********************** 



// Original image 
$filename = $_FILES['myFile']['tmp_name']; 

// 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 = $current_width/2 - 320; 
$top = $current_height/2 - 320; 

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

// 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); 


    //********************* 
//crop image into sqaure 
//********************** 

私が悪いいずれかが、私がしたい助けることができれば、それは、 を動作しない原因と、これをコード化している場合、私は思ったんだけど:あなたは私の設計に不可欠なオプションがある「写真を撮る」オプションを使用します非常に感謝しています!ありがとう!

これはデスクトップでは動作しますがモバイルでは動作しないコードです。上記の最初のhtmlエントリはモバイルとデスクトップで動作します。

<?php 

error_reporting(0); 

$sUploadDirectory = 'uploads/'; 

$aFileTypesAllowed = array('image/jpeg', 'image/gif', 'image/png'); 
$aExtensionsAllowed = array('gif', 'png', 'jpg', 'jpeg'); 

$aJSExtensions  = 'new Array("gif", "png", "jpg", "jpeg")'; 

$bImagesOnly = true; 
$iMaxFileSize = 4194304; 

if(isset($_REQUEST['upload']) && $_REQUEST['upload'] == 'true') { 

$temp = explode(".", $_FILES["myFile"]["name"]); 
$newfilename = round(microtime(true)) . '.' . end($temp); 

$bSuccess = true; 
$sErrorMsg = 'Thanks! Your image was successfully uploaded.'; 

if (array_search($_FILES['myFile']['type'], $aFileTypesAllowed) === false || 
     !in_array(end(explode('.', strtolower($_FILES['myFile']['name']))), $aExtensionsAllowed)) { 

     $bSuccess = false; 
     $sErrorMsg = 'Invalid file format. Acceptable formats are: ' . implode(', ', $aFileTypesAllowed); 


    } else if ($bImagesOnly && !(getimagesize($_FILES['myFile']['tmp_name']))) { 

     $bSuccess = false; 
     $sErrorMsg = 'The image is invalid or corrupt. Please select another.'; 



    } else if ($_FILES['myFile']['size'] > $iMaxFileSize) { 

     $bSuccess = false; 
     $sErrorMsg = 'The file size of your property photo must not exceed ' . ($iMaxFileSize/1024) . 'Kb. Please try again.'; 

    } else { 


$filename = $_FILES['myFile']['tmp_name']; 

// 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 = $current_width/2 - ($current_width/2); 
$top = $current_height/2 - ($current_width/2); 

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

// 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); 





     if ([email protected]_uploaded_file($_FILES['myFile']['tmp_name'], $sUploadDirectory . $newfilename)) { 
      $bSuccess = false; 
      $sErrorMsg = 'An unexpected error occurred while saving your uploaded photo. Please try again.'; 
     } 




    } 



    print '<html>' . 
       '<head>' . 
        '<script type="text/javascript">' . 
         'parent.uploadResult(' . ($bSuccess ? 'true' : 'false') . ', \'' . $sErrorMsg . '\', \'' . $sUploadDirectory . $newfilename . '\');' . 
        '</script>' . 
       '</head>' . 
       '<body></body>' . 
      '</html>'; 


    die(); 

} 



?> 
+0

あなたが試したこと、うまくいかなかったこと、そして最終結果がどんなものになるかについて、さらに詳しい情報を提供できますか? –

+0

@F.StephenQ応答に感謝します。上のコードは、すべてのアスペクト比の画像をサーバーフォルダ 'uploads'にアップロードすることができます。私は正方形ではない画像(1:1)をアップロード時に正方形に切り抜く必要があります。私はこのようないくつかのサムネイルのチュートリアルを使用してみました:http://stackoverflow.com/questions/27094895/create-square-11-thumbnail-in-phpしかし、幸運を持っていない – BernieHm

答えて

1

ネイティブのPHP imagecrop関数を使用して、配列を使用してイメージサイズをカットすることができます。かなりシンプル。

画像が正方形の場合、何もしません。ただし、画像の幅が「高さ」より大きい場合は、幅を高さに合わせて中央に配置し、その逆もあります。

$file  = $_FILES['myFile']['name']; // get file name 
$fileext = strtolower(end(explode('.', $file))); // get file extension 
$filetmp = $_FILES['myFile']['tmp_name']; //get file tmp name 

switch ($fileext) { // check file extension using switch function 
    case 'jpg': 
    case 'jpeg': 
     $image = imagecreatefromjpeg($filetmp); 
    break; 
    case 'png': 
     $image = imagecreatefrompng($filetmp); 
    break; 
    case 'gif': 
     $image = imagecreatefromgif($filetmp); 
    break; 
    default: 
     $sErrorMsg = 'Invalid file format. Acceptable formats are: ' . implode(', ', $aFileTypesAllowed); 
} 

list($w, $h) = getimagesize($filetmp); // get image resolution 

if ($w < $h){ // if width is less than height, crop height using imagecrop function 
    $image = imagecrop($image, array(
     "x" => 0, 
     "y" => ($w - $h)/2, 
     "width" => $w, 
     "height" => $w 
    )); 
} else if ($h < $w){ // vice versa 
    $image = imagecrop($image, array(
     "x" => ($w - $h)/2, 
     "y" => 0, 
     "width" => $h, 
     "height" => $h 
    )); 
} 

if($_FILES['myFile']['size'] > 102400){ 
    $sErrorMsg = 'The file size of your property photo must not exceed ' . ($iMaxFileSize/1024) . 'Kb. Please try again.'; 
} 

if(empty($sErrorMsg){ 
    // upload file to server 
} 

ファイルがすべての要件と一致する場合は、サーバーにアップロードできます。もちろん、あなたのニーズに合わせてスクリプトを編集することもできますが、基本的な考え方です!

こちらがお役に立てば幸いです。 :-)あなたが何か質問をしているならば、それらをコメントに残してください!

+0

こんにちは@Caelan Grgurovicありがとうそれを動作させるために参照してください、私はトリミングに関するセクションを取り出し、私のコードに配置して、tmpの名前への参照もそこにあったとそれは何もアップロードされないことを確認? – BernieHm

+0

私のコードは上記のデスクトップでは動作しますが、モバイルでは動作しません - 私は何かを追加する必要があるのか​​、それともモバイルで動作させるために何か違うのですか? – BernieHm

+0

G'day、@BernieHm!スイッチ関数をインクルードし、実際の 'move_uploaded_file'関数e.t.cで'// upload file to server'をスワップアウトしましたか? –

関連する問題