1

I want to create a image dynamically and upload it to a folder. I will be getting image for body,sleeve,collar and cuff dynamically from user selection. I am merging all these images to create a new image and this new image to uploaded in a folder. Am not able to upload the filename to folder and there is no errors displaying also but am able to generate the merged image.

Below is code,

    <?php
$body = "img1.jpg";
$sleeve = "img2.jpg";
$collar = "img3.jpg";
$cuff = "img4.jpg";
$outputImage = imagecreatetruecolor(800, 800);

$background = imagecolorallocate($outputImage, 0, 0, 0);
imagecolortransparent($outputImage, $background);

$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);

$first = imagecreatefrompng($body);
$second = imagecreatefrompng($sleeve);
$third = imagecreatefrompng($collar);
$fourth = imagecreatefrompng($cuff);

imagecopyresized($outputImage,$first,0,0,0,0,600,600,600,600);
imagecopyresized($outputImage,$second,0,0,0,0,600,600,600,600);
imagecopyresized($outputImage,$third,0,0,0,0, 600, 600, 600, 600);
imagecopyresized($outputImage,$fourth,0,0,0,0,600,600,600,600);

$filename = round(microtime(true)).'.png';

imagepng($outputImage, $filename);
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');

$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);
print_r(error_get_last());
if($ret)
{
echo "success";die;
}
else
{
echo "fail";die;
}
imagedestroy($outputImage);
?>echo "fail";die;
}
imagedestroy($outputImage);
?>
Mohan
  • 281
  • 2
  • 14

3 Answers3

0

you are using imagepng(), That saves file to particular location you are providing.

Syntax for imagepng() :

bool imagepng ( resource $image [, mixed $to [, int $quality [, int $filters ]]] )

where $to refers to location where you want to save your image (including filename).

So instead of

$filename = round(microtime(true)).'.png';

imagepng($outputImage, $filename);
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');

$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);

Try This,

$filename = round(microtime(true)).'.png';
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
imagepng($outputImage, DIR_IMAGE.$filename);

You don't need move_uploaded_file() anymore, because the image created is already in your server.
I also had similar problem while creating captcha for new users, and This solution worked.

Nirav Madariya
  • 1,470
  • 2
  • 24
  • 37
  • If you found this helpful, **Mark this as an answer**, and also upvote the same, so that others with the same problem can be benefited in future. – Nirav Madariya Jun 02 '17 at 13:52
0

move_uploaded_file won't work on $filename because it's not an uploaded file - it was just created by your code.

Try switching the two lines (like below) and adding your path:

define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
imagepng($outputImage, DIR_IMAGE . $filename);

And remove the move_uploaded_file line:

//$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);
timclutton
  • 12,682
  • 3
  • 33
  • 43
-2

You should try file_put_contents instead of move_uploaded_file.

file_put_contents('you_file_location/filename', IMAGE_OBJECT_HERE);

or you can also save file using fwrite function.

Rehman
  • 182
  • 1
  • 10