In the upload code, I save a copy to /tmp/, make a resized version, and then upload both via file_put_contents. Although the files aren't that big, it's taking FOREVER to transfer them (I split the code up into chunks and timed each one, seeing where the bottleneck was). Is there a "better" (read; faster) way to do so, or am I screwed?
It's also hard to believe it's a networking issue, since all the servers are in the same hosting location.
Code sample:
Code: Select all
// Grab uploaded pic
$thePic = fopen("$absolute_path$sletter$userid$ext", "rb");
$savePic = fread($thePic, filesize("$absolute_path$sletter$userid$ext"));
// Remove their old image (if they have one)
deleteftp("$ftp_path$sletter$userid$ext");
deleteftp("$ftp_path/thumbs/$sletter$userid$ext");
// URL for new image
$ftpu = "ftp://$ftp_username:$ftp_password@" . $imageip . "/$ftp_path$sletter$userid$ext";
if(!file_put_contents($ftpu, $savePic))
err("Cannot copy to FTP!", 1);
$s = 50;
$image = $absolute_path . $sletter . $userid . $ext;
$img = explode(".",$image);
$ext = $img[count($img)-1];
if(($ext == 'gif') || ($ext == 'GIF')){
$source = imagecreatefromgif($image);
$ctype = "gif";
}
else if(($ext == 'png') || ($ext == 'PNG'))
{
$source = imagecreatefrompng($image);
$ctype = "png";
}
else if(($ext == 'jpg') || ($ext == 'JPG') || ($ext == 'jpeg') || ($ext == 'JPEG')){
$source = imagecreatefromjpeg($image);
$ctype = "jpeg";
}
if($source)
{
$source_width = imagesx($source);
$source_height = imagesy($source);
if($source_width > $source_height){
$width = $s;
$percent = $width / $source_width;
$height = $source_height * $percent;
}
else{
$height = $s;
$percent = $height / $source_height;
$width = $source_width * $percent;
}
$thumb = imagecreatetruecolor($width,$height);
imagecopyresized($thumb,$source,0,0,0,0,$width,$height,$source_width,$source_height);
}
else
{
$thumb = imagecreatetruecolor($s,$s);
$black = ImageColorAllocate($thumb,0,0,0);
$white = ImageColorAllocate($thumb,255,255,255);
imagefill($thumb,0,0,$black);
imagerectangle($thumb,0,0,$s,$s,$white);
}
if(($ext == 'gif') || ($ext == 'GIF')){
imagegif($thumb, $absolute_path . $sletter . $userid . "_thumb." . $ext);
}
else if(($ext == 'png') || ($ext == 'PNG'))
{
imagepng($thumb, $absolute_path . $sletter . $userid . "_thumb." . $ext);
}
else if(($ext == 'jpg') || ($ext == 'JPG') || ($ext == 'jpeg') || ($ext == 'JPEG')){
imagejpg($thumb, $absolute_path . $sletter . $userid . "_thumb." . $ext);
}
// Free up memory
imagedestroy($thumb);
imagedestroy($source);
// Transfer thumbnail to image server
$ftpu = "ftp://$ftp_username:$ftp_password@" . $imageip . "/$ftp_path/thumbs/$sletter$userid.$ext";
$thumbFile = fopen($absolute_path . $sletter . $userid . "_thumb." . $ext, "rb");
$thumbData = fread($thumbFile, filesize($absolute_path . $sletter . $userid . "_thumb." . $ext));
file_put_contents($ftpu, $thumbData);
// Remove original and thumb from current node (no need for dupes)
unlink("$absolute_path$sletter$userid$ext");
unlink($absolute_path . $sletter . $userid . "_thumb." . $ext);