It is impossible to do this with just PHP (excluding methods relating to DOM loading and HTML, the recommended way would be to use a watcher and fire an event on detection of image loading completion).
There are however, ways you can speed up image loading in PHP:
Ensure that images are stored in directory and not as BLOB
If you are storing your images in some hardcoded format in your database, and not being directly accessed from your directory, then you should stop doing this. This dramatically increases load time and rendering the image.
Resizing done locally
Do a resize on the image directly before uploading it to the server. You can do this by using PHP built in functions. Something like this:
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = AVATAR_IMAGE_WIDTH;
$ratio = $dest_imagex / $source_imagex;
$dest_imagey = $ratio * $source_imagey;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
$image = imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
You can calculate the pre-size image width and height and scale appropiately to maintain the aspect ratio. The constant AVATAR_IMAGE_WIDTH
can be set to anything (or AVATAR_IMAGE_HEIGHT
), or you can adjust the variables to your liking. This will prevent CSS having to load the DOM and load the image in full size and then resize it into whatever you want.