2017-08-23 23 views
0

私は、base64経由でサーバーに写真をアップロードするiOSアプリを持っています。PHPクロップ画像正方形黒写真結果

イメージを通常の場所にアップロードしてから、PHPで四角に切り抜いて別の場所に移動します。

写真は完全に切り取られており、サーバーにも正しくアップロードされていますが、新しい写真(四角形)は黒で、解決策が見つかりませんでした。

PHPコード:

// Check if file was upload successfully 
       if (file_exists($temporaryPath)) { 
        // Rsize and reduce photo size 
        list($width, $height) = getimagesize($temporaryPath); 
        $myImage = imagecreatefromjpeg($temporaryPath); 
        if ($width > $height) { 
         $y = 0; 
         $x = ($width - $height)/2; 
         $smallestSide = $height; 
        } else { 
         $x = 0; 
         $y = ($height - $width)/2; 
         $smallestSide = $width; 
        } 
        $thumbSize = 1080; 
        $thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
        imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 
        imagejpeg($thumb, $imagePath, 100); 

        // Check if new image exists 
        if (file_exists($imagePath)) { 
         // Upload success 
         $response = array(
          "id"=>"NULL", 
          "msg"=>"success" 
         ); 
         echo json_encode(array("user_data"=>$response));  
         exit(); 
        } else { 
         // Failed to resize photo 
         $response = array(
          "id"=>"NULL", 
          "msg"=>"error" 
         ); 
         echo json_encode(array("user_data"=>$response));  
         exit(); 
        } 
       }else{ 
        // Upload failed 
        $response = array(
         "id"=>"NULL", 
         "msg"=>"upload_failed" 
        ); 
        echo json_encode(array("user_data"=>$response));  
        exit(); 
       } 

変数$テンポラリは、現在の写真が一時的にアップロードへのパスです。

どうすればよいですか?

ありがとうございました!

+0

アップロードした画像(つまり、 '$ temporaryPath'の画像)が有効な画像であることを実際に確認しましたか? 'imagecreatefromjpeg'によって正しく読み込まれているかどうかを確認するために、実際にはJPEGですか?' width'、 'height'、' height'の計算値は、 'x'、' y'、 'smallestSide'は正しいですか?実際に画像は1080x1080よりも大きくなっていますか? – jcaron

+0

iOSはPNGで一時的には.jpgとして保存します。この例は1080x1080より大きい –

+0

あなたはそれを.jpgとして名前を変更することを意味しますが、実際にはpngですか?imagecreatefromjpegは実際のJPEGを期待していますか? – jcaron

答えて

0

@jcaronのおかげで、私はこの問題を解決しました。私は画像をPNG形式で保存していましたが、imagecreatefromjpegをimagecreatefrompngに置き換えました。

関連する問題