Jump to content

Odd file permissions problem with imagejpeg


rainjam

Recommended Posts

I think it's file permissions, anyway.

 

I'm transferring a CMS for a site (www.perfectskindirect.co.uk) from its current IIS home onto a dedicated Linux server. This has caused a couple of hiccups with file permissions and other stuff.

 

The "add a new product" section works fine: it has an image upload function which takes an image and creates four square versions of it: 240x240, 120x120, 84x84 and 60x60. So I'm pretty sure it's not the code.

 

When you're editing an existing product, though, and you want to change the image, it won't let you do it, although it uses most of the same code. It seems to be creating new image files with the permissions

 

-rw-r--r--

 

(which would be 644 I think?)

 

and although move_uploaded_file seems to work ok on the replacement file, the following (pretty standard) function, which is called like this

 

<?php
  if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
     // $uploadfile is a full file path, ie /var/path/to/temp/folder/file.ext
     $filename = do_psd_big_image($uploadfile, $thisID);
     
     // since by this point there's a valid record we just need to update it with the image filename
     $image_sql = "UPDATE Masters SET MasterImage = '" . $filename . "' WHERE MasterID = " . $thisID;
     $imresult = mysql_query($image_sql);
     
  }
?>

 

doesn't do what it does when you're creating something from scratch:

 

<?php
function do_psd_big_image($file, $id) {
  // $id is a valid ID number for a product
  // $file is a valid filename with full path, which we want to do the following things to:
  //
  //  1. determine the size
  //    2a. if it's smaller than 240x240, fit it into a 240x240 white square
  //    2b. if it's bigger, resize it into a 240x240 white square
  //  3. save this white square as an image in /240/
  //  4. resize it to 120x120 and save that in /120/
  //  5. resize to 84x84, save in /84/
  //  6. resize this to 60x60 and save in /60/
  //
  //  we can return the filename (no path) as it will be the same in all folders

  list($width, $height) = getimagesize($file);
  
  // create array with image sizes in it
  $dims = array(240, 120, 84, 60);

  // first we need to make a square image to plop it into
  $sq_dim = (($width > $height) ? ($width) : ($height));
  $image_s = imagecreatetruecolor($dims[0], $dims[0]);

  // fill with white
  $white = imagecolorallocate($image_s, 255, 255, 255);
  imagefill($image_s, 0, 0, $white);
  
  // recreate the original image
  $image = imagecreatefromjpeg($file);

  // in case the original image is smaller than 240x240 we don't want to blow it up, so put it in the middle of the canvas
  if (max($width, $height) < $dims[0]) {
    // plop it in the middle, unresized
    $new_x = ($dims[0]/2) - ($width/2);
    $new_y = ($dims[0]/2) - ($height/2);
    imagecopyresampled($image_s, $image, $new_x, $new_y, 0, 0, $width, $height, $width, $height);
  } else {
    // resize it and fit it in the frame
    $new_width = (($width > $height) ? ($dims[0]) : ($dims[0] * ($width / $height)));
    $new_height = (($width > $height) ? ($dims[0] * ($height / $width)) : ($dims[0]));
    $new_x = ($dims[0]/2) - ($new_width / 2);
    $new_y = ($dims[0]/2) - ($new_height / 2);
    imagecopyresampled($image_s, $image, $new_x, $new_y, 0, 0, $new_width, $new_height, $width, $height);
  }

  $docroot = "/path/to/httpdocs";
  
  $image_name = $docroot . '/images/products/240/' . $id . '.jpg';
  imagejpeg($image_s, $image_name, 75);
  // chmod($image_name, 0776);
  
  // now do the other three images
  for ($i = 1; $i <= 3; $i++) {
    $image_t = imagecreatetruecolor($dims[$i], $dims[$i]);
    imagecopyresampled($image_t, $image_s, 0, 0, 0, 0, $dims[$i], $dims[$i], $dims[0], $dims[0]);
    $image_name = $docroot . '/images/products/' . $dims[$i] . '/' . $id . '.jpg';
    imagejpeg($image_t, $image_name, 75);
    // chmod($image_name, 0776);
    
    imagedestroy($image_t);
  }
  
  return $id . ".jpg";

}
?>

 

Can anyone help - why should this all work fine if the file doesn't already exist? I'm not at all a Linux expert, so I'm unable to tell if this script is running as the right user, if it has the permissions to set permissions, or if I'm on the wrong track entirely....

 

Edit: after you've tried to replace an image, if you browse directly to it you get a 403: Forbidden error.

 

Thanks in advance

 

Nick

I can't be bothered to read all that code  ;) ;) ;D ;D

 

But you need to let apache own the whole html directory structure:

 

chown -R apache.apache *

 

, on my machine that'd be /var/www/html... then give the correct permissions:

 

chmod -R 600

 

look at the man pages for exact syntax :-)

 

 

monk.e.boy

 

Thanks for that - don't blame you not wanting to wade thru the code  :o

 

so (sorry for the stupid question but I'm really not a linux expert) - if the httpdocs directory is at, say

 

/var/www/psd/httpdocs/

 

do i need to use chown -R apache.apache * so it includes httpdocs (and everything in it of course)?

 

then the same for the chmod?

 

Cheers

 

Nick

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.