I have the following code, that works very well. However I'm about to launch it to 5000 users and will no doubt take a beating early on. I want to ensure that if there are any opportunities to streamline so the code executes better or faster, that I do not miss them.
I've only pasted the PHP GD portion of the code, as the rest of my page is fine (some basic HTML)
$position = strpos($size, 'x');
$width = substr($size,0,$position);
$height = substr($size,$position+1);
$logo_image = imagecreatefrompng($image);
imagealphablending( $logo_image, false );
imagesavealpha( $logo_image, true );
$logo_width = imagesx($logo_image);
$logo_height = imagesy($logo_image);
if(($width>=601)&&($width<=1080)) {
$percent = $width/$logo_width;
} elseif($width>=1081) {
$percent = 1.2;
$font_size = 28;
}
$logo_new_width = $logo_width*$percent;
$logo_new_height = $logo_height*$percent;
$base_image = imagecreatetruecolor($width, $height);
$logo_x = (($width/2)-($logo_new_width/2));
$logo_y = (($height/2)-($logo_new_height/2));
$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[3]-$text_box[1];
$x = ($width/2) - ($text_width/2);
if($height==768) {
$y = (($height/2)+($logo_new_height/2));
} else {
$y = (($height/2)+($logo_new_height));
}
//set overall background color here based on user selection
if($theme=='plum') {
$fill_color = imagecolorallocate($base_image, 93, 62, 93);
} elseif($theme=='black') {
$fill_color = imagecolorallocate($base_image, 0, 0, 0);
} elseif($theme=='white') {
$fill_color = imagecolorallocate($base_image,255,255,255);
} elseif($theme=='gray') {
$fill_color = imagecolorallocate($base_image,149,150,153);
} elseif($theme=='charcoal') {
$fill_color = imagecolorallocate($base_image,78,80,84);
} elseif($theme=='warm') {
$fill_color = imagecolorallocate($base_image,255,79,31);
}
//Set the Text Color
if($textC=='black') {
$text_color = imagecolorallocate($base_image,0,0,0);
} elseif($textC=='red') {
$text_color= imagecolorallocate($base_image,255,79,31);
} elseif($textC=='white') {
$text_color = imagecolorallocate($base_image,255,255,255);
} elseif($textC=='plum') {
$text_color = imagecolorallocate($base_image, 93, 62, 93);
} elseif($textC=='gray') {
$text_color = imagecolorallocate($base_image,78,80,84);
}
imagefill($base_image, 0, 0, $fill_color);
imagesavealpha($base_image, TRUE);
//set text color here
imagettftext($base_image,$font_size+1,0,$x,$y,$text_color,$font,$text );
imagecopyresampled($base_image, $logo_image, $logo_x, $logo_y, 0, 0,$logo_new_width, $logo_new_height, $logo_width, $logo_height);
$num_str = "";
$charsRand = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<6;$i++)
$num_str.=substr($charsRand,rand(0,strlen($charsRand)),1);
$filename_create = substr($text,0,strpos($text, ".")).'_'.$num_str.'.png';
imagepng($base_image,'images_created/'.$filename_create,1);
$file_size = filesize('images_created/'.$filename_create);
Did I do anything that will cause me grief? Any tips for me to optimize this portion of the code ?
Look forward to any and all comments. This was my first go around with GD.