I have a scenario where a user uploads an image file. I strip the background color (defined as the upper left corner’s color)…and save to file with imagepng.
I originally tried echoing some html that directly called that file name, but it would take as long as seven minutes for the image to appear (this is a 6k file, nothing fancy). I read that you should not try to use the posted data in the same request, so, instead of outputting the image, I changed it to a link to a second page, which would get the image.
But the same lag occurs. I have not done much image manipulation, so appreciate any guidance as where I’ve gone wrong, or ways to speed things up. My eventual goal is that the user might be uploading 10+ images at a time
Here’s the primary code, the viewing_page.html was just <img src="target.png"/>
<?php
if($_FILES['test']['tmp_name']){
//some validation is necessary, this is just a proof of concept for a friendly user test
if($_FILES['test']['type']=="image/png"){
$im = imagecreatefrompng($_FILES['test']['tmp_name']);
}
if(($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")){
$im = imagecreatefromjpeg($_FILES['test']['tmp_name']);
}
if($_FILES['test']['type']=="image/gif"){
$im = imagecreatefromgif($_FILES['test']['tmp_name']);
}
//determine background RGB
$rgb = imagecolorat($im, 0, 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$background = imagecolorallocate($im, $r, $g, $b);
// Make the background transparent
imagecolortransparent($im, $background);
imagepng($im,'target.png');
imagedestroy($im);
echo "<a href=\"viewing_page.html\">View</a>";
//echo "<img src=\"target.png\"/>"; //-----------this was my first (and preferred) approach
}
else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body bgcolor="#00CCFF">
<form action="" method="post" enctype="multipart/form-data">
<input name="test" type="file" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
<?php
}
?>
Does imagepng really take so long to generate the .png file or does your web server not serve the image until it has 7 minutes of age? On my system (Linux, PHP 5.3.6) and with your code the image is available immediately.