0

ギャラリーに画像をアップロードするために使用するコードのスニペットがあります。コードの最後の部分に到達するまで、すべてが機能しているようです。

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
     $pubID = $_POST['pubID'];
     $size = 260; // the thumbnail height
     $filedir = '../images/gallery/'.$pubID.'/'; // the directory for the original image
     $thumbdir = '../images/gallery/'.$pubID.'/thumb/'; // the directory for the thumbnail image
     $maxfile = '2000000';
     $mode = '0777';
     $userfile_name = $_FILES['Gallimage']['name'];
     $userfile_tmp = $_FILES['Gallimage']['tmp_name'];
     $userfile_size = $_FILES['Gallimage']['size'];
     $userfile_type = $_FILES['Gallimage']['type'];
     if (isset($_FILES['Gallimage']['name']))
     {
         $prod_img = $filedir.$userfile_name;
         $prod_img_thumb = $thumbdir.$userfile_name;
         move_uploaded_file($userfile_tmp, $prod_img);
         chmod ($prod_img, octdec($mode));
         $sizes = getimagesize($prod_img);
         $aspect_ratio = $sizes[1]/$sizes[0]; 
         if ($sizes[1] <= $size)
         {
             $new_width = $sizes[0];
             $new_height = $sizes[1];
         }else{
             $new_height = $size;
             $new_width = abs($new_height/$aspect_ratio);
         }
         $destimg=ImageCreateTrueColor($new_width,$new_height)
             or die('Problem In Creating image');
         $srcimg=ImageCreateFromJPEG($prod_img)
             or die('Problem In opening Source Image');
         if(function_exists('imagecopyresampled'))
         {
             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
             or die('Problem In resizing');
         }else{
             Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
             or die('Problem In resizing');
         }
         ImageJPEG($destimg,$prod_img_thumb,90)
             or die('Problem In saving');
         imagedestroy($destimg);
     }

次の行にエラーが表示されます: ImageJPEG($destimg,$prod_img_thumb,90)。

エラーコードの84行目は次のとおりです:ImageJPEG($ destimg、$ prod_img_thumb、90)

Warning: imagejpeg() [function.imagejpeg]: Unable to open '../images/gallery/264/thumb/Hair-Salon-1.jpg' for writing: No such file or directory in /home/www/public_html/console/gallery.php on line 84. Problem In saving
4

2 に答える 2

0

もう少しエラーチェック、いくつかの修正、およびいくつかの一般的な改善を加えて、このコードを試してください。すべてがコメントされています。何もわからない場合は、次のように質問してください。

// Try not to over-use parenthesis, it makes code less readable. You only need them to group conditions.
if (isset($_POST["MM_insert"]) && $_POST["MM_insert"] == "form1") {

  // Pass through basename() for sanitization
  $pubID = basename($_POST['pubID']);

  $size = 260; // the thumbnail height
  $filedir = '../images/gallery/'.$pubID.'/'; // the directory for the original image
  $thumbdir = '../images/gallery/'.$pubID.'/thumb/'; // the directory for the thumbnail image
  $maxfile = '2000000';

  // PHP understands proper octal representations - no need to turn it into a string
  $mode = 0777;

  if (isset($_FILES['Gallimage']['name'])) {

    // Get upload info and create full paths
    $userfile_name = $_FILES['Gallimage']['name'];
    $userfile_tmp = $_FILES['Gallimage']['tmp_name'];
    $userfile_size = $_FILES['Gallimage']['size'];
    $userfile_type = $_FILES['Gallimage']['type'];
    $prod_img = $filedir.$userfile_name;
    $prod_img_thumb = $thumbdir.$userfile_name;

    // Create directories if they don't exist - this is the crux of your problem
    if (!is_dir($filedir)) {
      mkdir($filedir, $mode, TRUE)
        or die('Unable to create storage directory');
    }
    if (!is_dir($thumbdir)) {
      mkdir($thumbdir, $mode, TRUE))
        or die('Unable to create thumb directory');
    }

    // Move it to the correct location and set permissions
    move_uploaded_file($userfile_tmp, $prod_img)
      or die('Unable to move file to main storage directory');
    chmod($prod_img, $mode)
      or die('Unable to set permissions of file');

    // Get info about the image
    $sizes = getimagesize($prod_img);
    $aspect_ratio = $sizes[1] / $sizes[0]; 
    if ($sizes[1] <= $size) {
      $new_width = $sizes[0];
      $new_height = $sizes[1];
    } else {
      $new_height = $size;
      $new_width = abs($new_height / $aspect_ratio);
    }

    // Create image resources
    $destimg = imagecreatetruecolor($new_width, $new_height)
      or die('Problem in creating image');
    $srcimg = imagecreatefromjpeg($prod_img)
      or die('Problem in opening source Image');

    // Manipulate the image
    if (function_exists('imagecopyresampled')) {
      imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg))
        or die('Problem in resizing');
    } else {
      imagecopyresized($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg))
        or die('Problem in resizing');
    }

    // Save the thumbnail and destroy the resources
    imagejpeg($destimg, $prod_img_thumb, 90)
      or die('Problem in saving');
    imagedestroy($destimg);

  }

  // More code here? If not, merge all of this into a single if block      

}
于 2012-05-11T12:35:17.307 に答える