コード例 #1
0
 function wpp_get_image_link($attachment_id, $size, $args = array())
 {
     global $wp_properties;
     if (empty($size) || empty($attachment_id)) {
         return false;
     }
     //** Optional arguments */
     $defaults = array('return' => 'string');
     extract(wp_parse_args($args, $defaults), EXTR_SKIP);
     if (isset($wp_properties['configuration']['do_not_automatically_regenerate_thumbnails']) && $wp_properties['configuration']['do_not_automatically_regenerate_thumbnails'] == 'true') {
         //* If on-the-fly image generation is specifically disabled, we simply return the default URL */
         $default_return = wp_get_attachment_image_src($attachment_id, $size, true);
         $i[0] = $default_return[0];
         $i[1] = $default_return[1];
         $i[2] = $default_return[2];
     } else {
         //* Do the default action of attempting to regenerate image if needed. */
         $uploads_dir = wp_upload_dir();
         //** Get image path from meta table (if this doesn't exist, nothing we can do */
         if ($_wp_attached_file = get_post_meta($attachment_id, '_wp_attached_file', true)) {
             $attachment_path = $uploads_dir['basedir'] . '/' . $_wp_attached_file;
         } else {
             return false;
         }
         //** Get meta of main image (may not exist if XML import) */
         $image_meta = wp_get_attachment_metadata($attachment_id);
         //** Real URL of full image */
         $img_url = wp_get_attachment_url($attachment_id);
         //** Filenme of image */
         $img_url_basename = wp_basename($img_url);
         if (isset($image_meta['sizes'][$size]) && !empty($image_meta['sizes'][$size]['file'])) {
             //** Image image meta exists, we get the path and URL to the requested image size */
             $requested_size_filepath = str_replace($img_url_basename, $image_meta['sizes'][$size]['file'], $attachment_path);
             $requested_image_url = str_replace($img_url_basename, $image_meta['sizes'][$size]['file'], $img_url);
             $image_path = $requested_size_filepath;
             //** Meta is there, now check if file still exists on disk */
             if (file_exists($requested_size_filepath)) {
                 $requested_image_exists = true;
             }
         }
         if (isset($requested_image_exists) && $requested_image_exists) {
             $i[0] = $requested_image_url;
         } else {
             //** Image with the current size doesn't exist. Try generate file */
             if (WPP_F::generate_image($attachment_id, $size)) {
                 //** Get Image data again */
                 $image = image_downsize($attachment_id, $size);
                 if (is_array($image)) {
                     $i = $image;
                 }
             } else {
                 //** Failure because image could not be resized. Return original URL */
                 $i[0] = $img_url;
                 $image_path = str_replace($uploads_dir['baseurl'], $uploads_dir['basedir'], $img_url);
             }
         }
     }
     // Must check that $image_path exists, for remotely stored images this will be empty and will cause an an error within getimagesize.
     if (isset($image_path) && $image_path) {
         $getimagesize = @getimagesize($image_path);
     } else {
         $getimagesize = array('', '');
     }
     //** Get true image dimensions or returned URL */
     $i[1] = $getimagesize[0];
     $i[2] = $getimagesize[1];
     //** Return image data as requested */
     if ($i) {
         switch ($return) {
             case 'array':
                 if ($i[1] == 0 || $i[2] == 0) {
                     $s = WPP_F::image_sizes($size);
                     $i[1] = $s['width'];
                     $i[2] = $s['height'];
                 }
                 return array('link' => $i[0], 'src' => $i[0], 'url' => $i[0], 'width' => $i[1], 'height' => $i[2]);
                 break;
             case 'string':
             default:
                 return $i[0];
                 break;
         }
     }
     return false;
 }