0

I am attempting to upload company logo images to my Media Library - but I am noticing some problems...

Namely, some of those images seem to come in at 0Kb created.

Source image URLs are all like https://logo.clearbit.com/spotify.com?format=png

I wish to store them in /home/mysite/public_html/wp-content/uploads/org_logos/{companyname}.png

... wherein companyname is the slug of the corresponding company term for taxonomy Organisation.

Here is my code...

// Remote logo URL
$image_url = $json_object->logo.'?format=png';

// Generate a file path and name
$upload_dir = wp_upload_dir();
$image_data = file_get_contents( $image_url );
$filename = $company_term->slug.'.png';
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
  $file = $upload_dir['path'] . '/org_logos/' . $filename;
}
else {
  $file = $upload_dir['basedir'] . '/org_logos/' . $filename;
}

// Copy the file
file_put_contents( $file, $image_data );

// Add to Media Library
$wp_filetype = wp_check_filetype( $filename, null ); // eg. image/png
$attachment = array(
  'post_mime_type' => $wp_filetype['type'],
  'post_title' => sanitize_file_name( $filename ),
  'post_content' => '',
  'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );

require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );

This is successfully adding something to both the server and the Media Library every time. But, whilst sometimes that is an actual image, sometimes it is 0Kb and/or a blank thumbnail in the Media Library.

I had thought there was an issue around using slug where it contains dashes as a filename because the resulting image file for source https://logo.clearbit.com/starcomww.com?format=png (/home/mysite/public_html/wp-content/uploads/org_logos/starcom-mediavest-group.png) is always blank.

But, actually, the problem goes for more than just filenames which take slugs bearing dashes, because another problematic example is a no-dash source https://logo.clearbit.com/cadreon.com?format=png wherein file /home/mysite/public_html/wp-content/uploads/org_logos/cadreon.png is also 0Kb.

Image types are confirmed as image/png.

Is the problem my file_put_contents, my WordPress code or something else?

It's weird, because it works sometimes.

5
  • The problem is probably because the file_get_contents() request failed or ended up with no/invalid/non-image response. Try catching and logging failed requests - e.g. if ( ! $image_data ) { // log something }. You may also want to use the WordPress HTTP API (e.g. wp_remote_get()) which could tell you in case of HTTP/API errors encountered. – Sally CJ Jan 25 '19 at 9:30
  • @SallyCJ Seeing an echo from if ( ! $image_data ) { // log something } prompted me to try the remote image with and without the ?format=png parameter passed. Passing that string can lead to no image in some cases, even when this is a valid parameter - and, in fact, png is the default of two image options allowed. So it seems like a Clearbit problem, and I'm not even sure they're aware of it. I will fix by omitting passing a parameter, which leaves it default to the same. Thanks. – Robert Andrews Jan 25 '19 at 14:55
  • If https://logo.clearbit.com/cadreon.com?format=png sometimes results in a no image, then maybe it's an issue on the Clearbit side. Try consulting them. – Sally CJ Jan 26 '19 at 9:45
  • Also, I reverted to using file_get_contents and file_put_contents since I couldn't figure out how to save wp_upload_bits to a subfolder of "uploads". Still getting some phantom 0Kb images through both methods, so I do think it's an issue with Clearbit. Thanks. – Robert Andrews Jan 26 '19 at 12:39
  • I posted an answer there. I hope it helps you. – Sally CJ Jan 26 '19 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.