2017-10-26 13 views
0

このスクリプトは、アップロードされた画像を処理し、最大の高さまたは幅(どちらか長い方の長さ)が200ピクセルになるようにサイズ変更しても問題ありません。だから、それは完璧な正方形の画像、または200x140、または140x200だかどうかは今PHPでは、正方形以外の写真の周りに空白を追加すると、どのようにして200x200ピクセルになるのですか?

if(isset($_FILES['image'])) { 
     $img = $_FILES['image']['name']; 
     $tmp = $_FILES['image']['tmp_name']; 

     // get uploaded file's extension 
     $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION)); 

     //checking if image exists for this pool and removing if so, before adding new image in its place 
     if(file_exists("uploads/".$poolid.".png")) { 
     unlink("uploads/".$poolid.".png"); 
     } 

     // checks valid format 
     if(in_array($ext, $valid_extensions)) { 
     //re-size the image and make it a PNG before sending to server 
     $final_image = $poolid . ".png"; 
     $path = "uploads/".strtolower($final_image); 
     $size = getimagesize($tmp); 
     $ratio = $size[0]/$size[1]; // width/height 
     if($ratio > 1) { 
      $width = 200; 
      $height = 200/$ratio; 
     } 
     else { 
      $width = 200*$ratio; 
      $height = 200; 
     } 
     $src = imagecreatefromstring(file_get_contents($tmp)); 
     $dst = imagecreatetruecolor($width,$height); 
     imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
     imagedestroy($src); 
     imagepng($dst, $path); // adjust format as needed 
     imagedestroy($dst); 

     $_SESSION['image_uploaded']="yes"; 
     echo $path ."?".rand(1,32000); 
     } else { 
      echo 'invalid file'; 
     } 
    } 

など、200×200可能性があり、OpenGraphを使用してFacebookの共有は、少なくとも200x200の対象となる画像が必要です。そのため、140x200の画像は共有機能では機能しません。

私は正方形以外の画像を好きではないので、画像を撮りたいと思います。まだ正方形でない場合は、面(または上/下)に空白を追加したいと思います。毎回完璧な200x200の四角形として保存してください。

私はこれを試しましたが、動作していません(イメージが全く作成されません)。私がやろうとしたことに何が間違っていますか?これは過度に複雑には見えませんが、明らかに私は何かが欠けています。

if(isset($_FILES['image'])) { 
    $img = $_FILES['image']['name']; 
    $tmp = $_FILES['image']['tmp_name']; 

    // get uploaded file's extension 
    $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION)); 

    //checking if image exists for this pool and removing if so, before adding new image in its place 
    if(file_exists("uploads/".$poolid.".png")) { 
    unlink("uploads/".$poolid.".png"); 
    } 

    // checks valid format 
    if(in_array($ext, $valid_extensions)) { 
    //re-size the image and make it a PNG before sending to server 
    $final_image = $poolid . ".png"; 
    $path = "uploads/".strtolower($final_image); 
    $size = getimagesize($tmp); 
    $ratio = $size[0]/$size[1]; // width/height 
    if($ratio > 1) { 
     $width = 200; 
     $height = 200/$ratio; 
    } 
    else { 
     $width = 200*$ratio; 
     $height = 200; 
    } 
    $src = imagecreatefromstring(file_get_contents($tmp)); 
    $dst = imagecreatetruecolor($width,$height); 
    $orig_img=imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
    imagedestroy($src); 

    // create new image and fill with background colour 
    $new_img = imagecreatetruecolor($output_w, $output_h); 
    $bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red 
    imagefill($new_img, 0, 0, $bgcolor); // fill background colour 

    // copy and resize original image into center of new image 
    $final_img=imagecopyresampled($new_img, $orig_img, 0, 0, 0, 0, 200, 200, $width, $height); 
     imagepng($final_img, $path); // adjust format as needed 

    imagedestroy($dst); 
    $_SESSION['image_uploaded']="yes"; 
    echo $path ."?".rand(1,32000); 
    } else { 
     echo 'invalid file'; 
    } 
} 
+0

'imagecopyresampled'は別のイメージリソースではなくブール値を返します。 – Matey

答えて

1

一時的な中間イメージは必要ありません。背景を塗りつぶした後、再サンプリングされたソースイメージをコピー先のイメージに貼り付けることができます。ここをクリックしてください:

$src = imagecreatefromstring(file_get_contents($tmp)); 

    // Create new image and fill it with background color 
    $dst = imagecreatetruecolor($output_w,$output_h); 
    $bgcolor = imagecolorallocate($dst, 255, 0, 0); 
    imagefill($dst, 0, 0, $bgcolor); 

    // Copy resampled src image into dst 
    if ($ratio > 1) 
    imagecopyresampled($dst, $src, 0, ($output_h - $height)/2, 0, 0, $width, $height, $size[0], $size[1]); 
    else 
    imagecopyresampled($dst, $src, ($output_w - $width)/2, 0, 0, 0, $width, $height, $size[0], $size[1]);  

    imagepng($dst, $path); // adjust format as needed 

    imagedestroy($src); 
    imagedestroy($dst); 
関連する問題