Upload resize not working Ubuntu


#1

Hello,
i have this image upload and resize code that makes multiple size images and inserts it in database
i had it working on other 2 servers but now on Ubuntu Server 16.04 LTS it saves just main image (content) ans saves "" in resized columns.
Any of imagejpeg returns false!
What problem could i have here?

		$object = htmlentities($_POST['object'], ENT_QUOTES, "UTF-8");
	
		if($_FILES['capture']['size'] > 0)	{
			$fileName = $_FILES['capture']['name'];
			$tmpName  = $_FILES['capture']['tmp_name'];
			$fileSize = $_FILES['capture']['size'];
			$fileType = $_FILES['capture']['type'];

			$fp      = fopen($tmpName, 'r');
			$content = fread($fp, filesize($tmpName));
			$content = addslashes($content);
			fclose($fp);

			if(!get_magic_quotes_gpc())	{
				$fileName = addslashes($fileName);
			}

			$extension = getExtension($fileName);
			$extension = strtolower($extension);
			
			if($extension=="jpg" || $extension=="jpeg" ){
				$src = imagecreatefromjpeg($tmpName);
			}else if($extension=="png"){
				$src = imagecreatefrompng($tmpName);
			}else{
				$src = imagecreatefromgif($tmpName);
			}

			list($width,$height)=getimagesize($tmpName);

			if ($height<$width){
				$newwidth=500; $newheight=($height/$width)*$newwidth;
				$newwidth1=300; $newheight1=($height/$width)*$newwidth1;
				$newwidth2=150; $newheight2=($height/$width)*$newwidth2;
			}else{
				$newheight=400; $newwidth=($width/$height)*$newheight;
				$newheight1=200; $newwidth1=($width/$height)*$newheight1;
				$newheight2=150; $newwidth2=($width/$height)*$newheight2;
			}

			$thumb_width = 210;
			$thumb_height = 115;

			$original_aspect = $width / $height;
			$thumb_aspect = $thumb_width / $thumb_height;

			if ( $original_aspect >= $thumb_aspect ){
			   $new_height = $thumb_height; $new_width = $width / ($height / $thumb_height);
			}else{
			   $new_width = $thumb_width; $new_height = $height / ($width / $thumb_width);
			}

			$tmp=imagecreatetruecolor($newwidth,$newheight);
			$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
			$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
			$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
			imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
			imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);
			imagecopyresampled($thumb, $src, 0 - ($new_width - $thumb_width) / 2, 0 - ($new_height - $thumb_height) / 2, 0, 0, $new_width, $new_height, $width, $height);
			
			$big = "images/big_". $_FILES['capture']['name'];
			$middle = "images/middle_". $_FILES['capture']['name'];
			$small = "images/small_". $_FILES['capture']['name'];
			$ico = "images/ico_". $_FILES['capture']['name'];
			
			imagejpeg($tmp,$big,100);
			imagejpeg($tmp1,$middle,100);
			imagejpeg($tmp2,$small,100);
			imagejpeg($thumb,$ico,80);			

			
			$fp = fopen($big, 'r');
			$big = addslashes(fread($fp, filesize($big)));
			fclose($fp);
			
			$fp = fopen($middle, 'r');
			$middle = addslashes(fread($fp, filesize($middle)));
			fclose($fp);
			
			$fp = fopen($small, 'r');
			$small = addslashes(fread($fp, filesize($small)));
			fclose($fp);
			
			$fp = fopen($ico, 'r');
			$ico = addslashes(fread($fp, filesize($ico)));
			fclose($fp);

			
			unlink("images/big_". $_FILES['capture']['name']);
			unlink("images/middle_". $_FILES['capture']['name']);
			unlink("images/small_". $_FILES['capture']['name']);
			unlink("images/ico_". $_FILES['capture']['name']);

		}

Thanks!


How to upload and display image
Upload Image - create thumb
#2

Try adding this DEBUG script at the top of the file:

<?php

declare (strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

I think the problem may be with the functions getExtension($file) and get_extension($file);


#3

@John_Betong
ok i got error:

Fatal error: Uncaught TypeError: imagecreatetruecolor() expects parameter 2 to be integer, float given in my_file.php:390 Stack trace: #0 my_file.php(390): imagecreatetruecolor(500, 123.07692307692) #1 {main} thrown in my_file.php on line 390

390 line is the same line as $tmp=imagecreatetruecolor($newwidth,$newheight);

I can read the error but have no idea how to fix this!


#4

Try adding this line to convert the float to an integer:

$newheight = intval( $newheight );

#5

@John_Betong But why would it work on 2 other servers but here having this issue?


#6

Just guessing but it could be:
1. PHP versions are not the same
2. error_reporting is more strict preventing the function from operating correctly
3. PHP php.ini error_reporting set to 0 and prevent users seeing errors and warnings

The error log files may be helpful:

// should return with the error log file and path name..
echo ini_get( 'error_log' );

#7

@John_Betong after doing this i got :

Warning: imagejpeg(images/myimage.jpg): failed to open stream: Permission denied in myfile.php on line 414

Warning: imagejpeg(images/myimage.jpg): failed to open stream: Permission denied in myfile.php on line 415

Warning: imagejpeg(images/myimage.jpg): failed to open stream: Permission denied in myfile.php on line 416

Warning: fopen(images/myimage.jpg): failed to open stream: No such file or directory in myfile.php on line 435

Warning: filesize(): stat failed for images/myimage.jpg in myfile.php on line 436

Fatal error: Uncaught TypeError: fread() expects parameter 1 to be resource, boolean given in myfile.php:436 Stack trace: #0 myfile.php(436): fread(false, false) #1 {main} thrown in myfile.php on line 436

#8
  1. yeah now is PHP Version 7.0.22-0ubuntu0.16.04.1 was under PHP 6 could be something wrong with that?
  2. how could error_reporting prevent it from working?

Thanks


#9

Considering there was no PHP version 6 ever released there is something wrong with that somewhere.


#10

I just did not mented precise version for one where it worked it was PHP Version 5.6.24 (under 6 :D)


#11

The permission denied warning means the folder needs write permissions, set folder permission to 0777 if using FileZilla. Right click on the folder name and select permissions from the drop-down menu.

Tapped on a tablet :frowning:


#12

But i upload file from my PC to insert it in mysql database why and where from there some folder comes?


#13

The script you supplied uploads an image and creates four imagesl Big, middle, small and ico in the images folder.

The images are then deleted, unlinked.

I do not see where the files are stored in your database.


#14

I thought that i do not need to show that part also i did mentioned that on my post!


#15

Did you check to see if the relative path to images exist and the folder permissions are set to 0777?

It looks like the images are being created in memory but cannot be saved.

It is essential to get that part working before proceeding to the next stage in the script.


#16

Are you sure you want permissions to be 777 ?

It might be OK for a localhost or extremely short amounts of time for testing reasons I guess, but "letting everything and anything do everything and anything" sounds risky to me.

http://php.net/manual/en/security.filesystem.php


#17

The original script was not working and there were no errors or warnings shown. Adding the error_reporting and display_errors showed the errors. Servers usually have these features turned off and warnings will not show that the script did not work correctly.


#18

I have not found a solution that works on the server without manually setting the permissions to octal 0777 :frowning:

Locally I can use PHP to modify permissions before and after saving the images by setting chmod('path',0777); to allow writing. Afterwards revert the settings to something more secure. Online refused to accept the chmod(...); request and also trying the chgrp(...); fails.

I have also googled unsuccessfully for a solution :frowning:


#19

I was curious and created an online version and was disappointed with the PHP GD2 image quality.

I prefer to use ImageMagick

Online Demo

<?php

declare (strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'false');

$TITLE = 'John_Betong\'s ThumbNail Creator';
$forum = '#';
$date  = date('l, jS M Y - H:i:s');

$hdr = <<< ____TMP
<!DOCTYPE HTML>
<html lang="en">
<head>
<title> $TITLE </title>
<style>
body{
  font: 16px/1.42 Montserrat,BlinkMacSystemFont, -apple-system, 
  "Segoe UI",Roboto,Helvetica,Arial,sans-serif,
  Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans",
  "Helvetica Neue",Arial,sans-serif;
  /*
   font-smooth:always;
   -webkit-font-smoothing=antialiased;
  */ 
  border:0; margin:0; padding:0; 
}
h1,h2,h3,h4,h5,h6{
  font-weight:400;
  font-family:-apple-system,BlinkMacSystemFont,
  "Segoe UI",Roboto,Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans",
  Inconsolata,"Helvetica Neue",Arial,sans-serif;}
a {text-decoration:none;}
.bd8 {border:solid 1px #888;}
.bge {background-color:#eee;}
.btn {margin:0 1em; padding:0.21em 0.42em; background-color:#eee; color:#000;}
.clb {clear:both;}
.flr {float: right;}
.fss {font-size:small;}
.fwb {font-weight:700;}
.mga {margin:0 auto;}
.ooo {margin:0; padding:0;}
.POF {position:fixed; left:0; bottom:0;}
.p42 {padding: 0.42em;}
.tac {text-align:center;}
.w88 {width:88%; max-width:888px;}
</style>
</head>
<body>

<h1> 
  <a href="?"> 
    $TITLE 
    <sup class="fss">Source</sup>
  </a>
</h1>
<h5 class="ooo"> $date </h5>
<h5 class="flr ooo"> 
  <a href="$forum"> SitePoint Forum </a>
</h5>
<hr class="clb">
____TMP;
echo $hdr;


# SET GET PARAMETER
  $imgParam = $_GET['fff'] ?? NULL;
  if( isset($_FILES['capture']) ):
    # NO PROBLEM
  else:
    # CREATE FILES  
    $_FILES['capture'] = [
      'capture'   => 'capture',
      'name'      => $imgParam,
      'tmp_name'  => $imgParam,
      'type'      => 'type',
      'size'      => 424242,
    ];
  endif;  

# DISPLAY LINKS
  getLinksToImages();

if($imgParam):
  echo '<div class="w88 mga">';
    makeThumbnails($aFILES = $_FILES);
    showThumbnails($aFILES['capture']['name']);
  echo '</div>';// class="w88 mga">';
else:
  echo '<dl>';
    echo '<dt>Source: </dt>';
    echo '<dd class="bd8 p42">';
      highlight_file(__file__);
    echo '</dd>';
  echo '</dl>';  
endif;

echo '<p> <br> </p>';
  echo '<div class="POF tac fwb bge">';
    echo 'Wonderful place for a footer';
  echo '</div>';  

echo '</body></html>';



# ONLY FUNCTION BELOW

//=================================================================
function makeThumbnails($aFILES)
{
    if($_FILES['capture']['size'] > 0)
    {
      $fileName = $_FILES['capture']['name'];
      $tmpName  = $_FILES['capture']['tmp_name'];
      $fileSize = $_FILES['capture']['size'];
      $fileType = $_FILES['capture']['type'];

      $fp      = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
      fclose($fp);

      if(!get_magic_quotes_gpc()) {
        $fileName = addslashes($fileName);
      }

      $extension = getExtension($fileName);
      $extension = strtolower($extension);

      # JPG, PNG OR GIF4 
        if($extension=="jpg" || $extension=="jpeg" )
        {
          $src = imagecreatefromjpeg($tmpName);
        }else if($extension=="png"){
          $src = imagecreatefrompng($tmpName);
        }else{
          $src = imagecreatefromgif($tmpName);
        }

      list($width, $height) = getimagesize($tmpName);
      if ($height<$width){
        $newwidth0 = 500; $newheight0 = ($height/$width)*$newwidth0;
        $newwidth1 = 300; $newheight1 = ($height/$width)*$newwidth1;
        $newwidth2 = 150; $newheight2 = ($height/$width)*$newwidth2;
        $newwidth3 =  80; $newheight3 = ($height/$width)*$newwidth3;
      }else{
        $newheight0 =400; $newwidth0 = ($width/$height)*$newheight0;
        $newheight1 =200; $newwidth1 = ($width/$height)*$newheight1;
        $newheight2 =150; $newwidth2 = ($width/$height)*$newheight2;
        $newheight3 = 80; $newwidth3 = ($width/$height)*$newheight3;
      }

      $thumb_width  = 2100;
      $thumb_height = 1150;
      $original_aspect = $width / $height;
      $thumb_aspect    = $thumb_width / $thumb_height;
      if ( $original_aspect >= $thumb_aspect ){
         $new_height = $thumb_height; 
         $new_width  = $width / ($height / $thumb_height);
      }else{
         $new_width  = $thumb_width; 
         $new_height = $height / ($width / $thumb_width);
      }

      $imgObj = imagecreatetruecolor( (int) $width, (int) $height );

      # CREATE Image Resources
        $tmp0   = imagecreatetruecolor( (int) $newwidth0,   (int) $newheight0);
        $tmp1   = imagecreatetruecolor( (int) $newwidth1,   (int) $newheight1);
        $tmp2   = imagecreatetruecolor( (int) $newwidth2,   (int) $newheight2);
        $tmp3   = imagecreatetruecolor( (int) $newwidth3,   (int) $newheight3);

      # MAYBE BETTER QUALITY  
      if( function_exists('imageantialias')):
        imageantialias($tmp0,true);
        imageantialias($tmp1,true);
        imageantialias($tmp2,true);
        imageantialias($tmp3,true);
      endif;  

      imagecopyresampled($tmp0, $src,0,0,0,0, (int) $newwidth0, (int) $newheight0, $width, $height);
      imagecopyresampled($tmp1, $src,0,0,0,0, (int) $newwidth1, (int) $newheight1, $width, $height);
      imagecopyresampled($tmp2, $src,0,0,0,0, (int) $newwidth2, (int) $newheight2, $width, $height);
      imagecopyresampled($tmp3, $src,0,0,0,0, (int) $newwidth3, (int) $newheight3, $width, $height);

      # ENSURE PARAMETERS ARE ALL INTEGERS
        $tmp_0 = 0 - ($new_width  - $thumb_width) / 2;
        $tmp_0 = (int) $tmp_0;

        $tmp_1 = 0 - ($new_width  - $thumb_width) / 2;
        $tmp_1 = (int) $tmp_1;
        
        $tmp_2 = 0 - ($new_height - $thumb_height) / 2;
        $tmp_2 = (int) $tmp_2;
        
        $tmp_3 = 0 - ($new_height - $thumb_height) / 2;
        $tmp_3 = (int) $tmp_2;

        $new_height = (int) $new_height;
        $height     = (int) $height;

      $ok = imagecopyresampled
      (
        $imgObj, 
        $src, 
        $tmp_1, 
        $tmp_2, 
        0, 
        0, 
        $new_width, 
        $new_height, 
        $width, 
        $height
      );
      if($ok):
        $big    = "images/big_"     . $_FILES['capture']['name'];
        $middle = "images/middle_"  . $_FILES['capture']['name'];
        $small  = "images/small_"   . $_FILES['capture']['name'];
        $ico    = "images/ico_"     . $_FILES['capture']['name'];

        imagejpeg($tmp0,  $big,   100);
        imagejpeg($tmp1,  $middle,100);
        imagejpeg($tmp2,  $small, 100);
        imagejpeg($tmp3,  $ico,   100);      
  
        $fp  = fopen($big, 'r');
        $big = addslashes(fread($fp, filesize($big)));
        fclose($fp);
        
        $fp     = fopen($middle, 'r');
        $middle = addslashes(fread($fp, filesize($middle)));
        fclose($fp);
        
        $fp    = fopen($small, 'r');
        $small = addslashes(fread($fp, filesize($small)));
        fclose($fp);
        
        $fp  = fopen($ico, 'r');
        $ico = addslashes(fread($fp, filesize($ico)));
        fclose($fp);
    else:
        echo 'BIG PROBLEM WITH: ' .$_FILES['capture']['name'];
      endif;    

      # DELETE IMAGES???
        if(0):
          unlink("images/big_".     $_FILES['capture']['name']);
          unlink("images/middle_".  $_FILES['capture']['name']);
          unlink("images/small_".   $_FILES['capture']['name']);
          unlink("images/ico_".     $_FILES['capture']['name']);
        endif;  
    }
}

//=========================================================
function getExtension($file)
{
 $file      = substr($file, -5); 
 $extension = strstr( $file, '.' );
 $extension = substr( $extension, 1);
 
 return $extension ? $extension : false;
}    

//=========================================================
function showThumbnails($img)
{
  # CREATE THUMBS
    $sizes = ['ico', 'small', 'middle', 'big' ];
    foreach( $sizes as $id => $size ):
      echo  $img .'<br>';
      $tmp = 'images/' .$size .'_' .$img;
      $dim = getimagesize($tmp);
      echo '<img src="' .$tmp .'" alt="' .$tmp .'" /><br>';
      echo $dim[3] .'<br><br>';
    endforeach;  

  # ORIGNAL IMAGE
    echo '<br>Original image:';
    $tmp = $img;
    echo '<img src="' .$tmp .'" alt="' .$tmp .'" /><br>';
    $dim = getimagesize($tmp);
    echo $dim[3] .'<br><br>';

}

//=========================================================
function getLinksToImages()
{
# LOAD IMAGE IN THIS FOLDER
  $fileNames = glob('*.*');
  echo '<div style="text-align:center;">';
    echo '<ul>';
      foreach($fileNames as $id => $img):
        if( strpos($img, '.php') ):
          // PHP IS NOT AN IMAGE 
        else:  
          echo '<li style="display:inline-block;"> &nbsp; &nbsp;'
              .   '<a href="?fff=' .$img .'"><b class="btn">' .$img .'</b></a>'
              . '</li>';
        endif;      
      endforeach;  
    echo '</ul>';
  echo '</div>';  
}

#20

I have not tried yet as some other jobs are more important now :frowning:
Will try ASAP

Thanks