0

I am trying to resize image on upload. I had working upload code and added some code for resize that i found. Imagejpeg() returns false, so there is probably a problem. But I don't know what could it be.

function processImg($id){
$file_result ="";
if($_FILES['image']['error'] > 0){
    $file_result .="No file uploaded or Invalid File";
    $file_result .= "Error Code: " . $_FILES['image']['error'] ."<br>";
    //header('Location: products.php');
    exit();
}
$path =  $_SERVER['DOCUMENT_ROOT'] . "/shop/imgs/" .$id."."."jpg";
$tmp = resize(308,173, $path);


/*if(!move_uploaded_file($tmp, $path)){
    ?> <script> console.log("ahoj") </script> <?php
}*/
return $tmp;
} 

function resize($width, $height, $path){
  /* Get original image x y*/
  list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
  /* calculate new image size with ratio */
  $ratio = max($width/$w, $height/$h);
  $h = ceil($height / $ratio);
  $x = ($w - $width / $ratio) / 2;
  $w = ceil($width / $ratio);
  /* read binary data from image file */
  $imgString = file_get_contents($_FILES['image']['tmp_name']);
  /* create image from string */

  $image = imagecreatefromstring($imgString);


  $tmp = imagecreatetruecolor($width, $height);
  imagecopyresampled($tmp, $image,
    0, 0,
    $x, 0,
    $width, $height,
    $w, $h);
 $data = getimagesize($_FILES['image']['tmp_name']);
 var_dump($data);
$img = imagejpeg($tmp, $path, 100);

return img;
}
?>

EDITed with error messages

Warning: imagejpeg(): open_basedir restriction in effect. File(/var/www/html/shop/imgs/60.jpg) is not within the allowed path(s): (/home/vhosts/predajveci.orgfree.com/:/tmp/:/usr/share/pear/) in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 47


Warning: imagejpeg(/var/www/html/shop/imgs/60.jpg): failed to open stream: Operation not permitted in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 47


Notice: Use of undefined constant img - assumed 'img' in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 49

Strict Standards: Only variables should be passed by reference in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/processproducts.php on line 98
rtom
  • 585
  • 1
  • 12
  • 26
  • Enable error reporting at the top of your script to get a descriptive error message i.e `error_reporting(E_ALL); ini_set('display_errors', 1); ` – Cyclonecode Sep 22 '16 at 08:38
  • Edited OP with error messages. So I don't have permisions to that folder or what ? – rtom Sep 22 '16 at 08:44
  • 1
    The error message are pretty self explanatory you don't have correct permission to access the file/folder. You could try to change the permission on the folder running `chmod -R 777 /var/www/html/shop/imgs` – Cyclonecode Sep 22 '16 at 08:45
  • I am using windows and my web is running on free hosting page, so i probably need to find permissions and change it there. – rtom Sep 22 '16 at 08:49
  • Possible duplicate of http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 22 '16 at 10:05
  • @VicSeedoubleyew - This has nothing to do with the above question. The problem is that the file/folder exists but he has not got the permission to access it. – Cyclonecode Sep 22 '16 at 16:52
  • @Cyclone, that is included in the above link – Vic Seedoubleyew Sep 22 '16 at 18:20
  • @VicSeedoubleyew - That might be so, but still the following error message `Failed to open stream : No such file or directory.` has nothing to do with file permissions. – Cyclonecode Sep 22 '16 at 18:30
  • @Cyclone, precisely it has. If you want to learn why you can refer to that link – Vic Seedoubleyew Sep 22 '16 at 19:16

0 Answers0