I've written a little function to take a url, and resize the image and store it on my local, however the script is taking about .85 seconds to run when it needs to create the folder, and .64 seconds on a resize. I currently have JPEG and PNG supported as seen below.
I'm wondering if there is a quicker method or something I'm doing that is taking to long, as the current times i have are unacceptable for me, I would really like to get this to execute faster.
Any thoughts / ideas are greatly appreciated.
Thank you!
function getTime() {
$timer = explode( ' ', microtime() );
$timer = $timer[1] + $timer[0];
return $timer;
}
function createThumb($thumb, $ids){
$start = getTime();
// File and new size
$filename = $thumb;
// Get new dimensions
$img1 = getimagesize($filename);
if ($img1[0] > $img1[1]) {
$percentage = ('72' / $img1[0]);
} else {
$percentage = ('72' / $img1[1]);
}
$new_width = $img1[0] * $percentage;
$new_height = $img1[1] * $percentage;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
if($img1['mime']=='image/png'){
$image = imagecreatefrompng($filename);
imagealphablending($image_p, false);
imagesavealpha($image_p,true);
$transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
imagefilledrectangle($image_p, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]);
}
else {
$image = imagecreatefromjpeg($filename);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]);
$imgPath = '/foo/bar/location/'.$ids;
$imgName ='';
//category, product, support
if(!is_dir($imgPath)) {
mkdir($imgPath, 0777);
chmod($imgPath, 0777);
}
if(!is_file($imgPath."/index.html")){
$ourFileName = $imgPath."/index.html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle,'<html><body>401</body></html>');
fclose($ourFileHandle);
}
// Output
if($img1['mime']=='image/png'){
$name = rand(1, 156406571337);
$imgName = date("y_m_d_h_m_s").$name.'.png';
imagepng($image_p, $imgPath.'/'.$imgName);
} else {
$name = rand(1, 156406571337);
$imgName = date("y_m_d_h_m_s").$name.'.jpg';
imagejpeg($image_p, $imgPath.'/'.$imgName, 100);
}
$end = getTime();
echo '<strong>createImage</strong>: '.round($end - $start,4).' seconden<br />';
exit;
return $imgName;
}