コード例 #1
0
 /**
  * 
  */
 public static function IPrepare($imgs, $posts, $meta_checkout = array(), $forceScan = false)
 {
     $iNotGood = array();
     $iNotGoodTotal = array();
     $metaNotGood = array();
     $metaNotGoodTotal = array();
     $upload_dir = wp_upload_dir();
     foreach ($posts as $post) {
         if (empty($post->post_content)) {
             continue;
         }
         $ifound = self::IScan($imgs, $post->post_content);
         if (count($ifound) < 1) {
             continue;
         }
         foreach ($ifound as $order => $img) {
             $iID = $img['id'];
             //Get image that its size is not good
             if (!(list($width_origin, $height_origin) = @getimagesize($img['src']))) {
                 continue;
             }
             $ratio_origin = $width_origin / $height_origin;
             $width = $img['width'];
             $height = $img['height'];
             //Check if img tag is missing with/height attribute value or not
             if (!$width && !$height) {
                 $width = $width_origin;
                 $height = $height_origin;
             } elseif ($width && !$height) {
                 $height = $width * (1 / $ratio_origin);
             } elseif ($height && !$width) {
                 $width = $height * $ratio_origin;
             }
             if ($width_origin > $width || $height_origin > $height) {
                 $img_before = str_replace(array($upload_dir['baseurl']), '', $img['src']);
                 $ibpart = ImageHelper::IGetPart($img_before);
                 $img_after = $ibpart->base_path . (!empty($ibpart->name_prefix) ? $ibpart->name_prefix : $ibpart->name) . '-' . $width . 'x' . $height . $ibpart->name_suffix . $ibpart->ext;
                 $destination = $upload_dir['basedir'] . '/' . $img_after;
                 $srcs = array('img_before_dir' => $upload_dir['basedir'] . $img_before, 'img_before_url' => $upload_dir['baseurl'] . $img_before, 'img_after_dir' => $upload_dir['basedir'] . $img_after, 'img_after_url' => $upload_dir['baseurl'] . $img_after);
                 $size = filesize($srcs['img_before_dir']) / 1024;
                 if ($size > 1024) {
                     $size = $size / 1024;
                     $sizes = 'MB';
                 } else {
                     $sizes = 'KB';
                 }
                 $size = @round($size, 1);
                 $iNotGood[$iID][$post->ID]['ID'] = $post->ID;
                 $iNotGood[$iID][$post->ID]['title'] = $post->post_title;
                 $iNotGood[$iID][$post->ID]['post_type'] = $post->post_type;
                 $iNotGood[$iID][$post->ID]['img_before_optm'][$order] = array('size' => $size, 'sizes' => $sizes, 'src' => $srcs['img_before_url'], 'width' => $width, 'height' => $height, 'dimension' => ImageHelper::IGetDimension($img_before));
                 $iNotGood[$iID][$post->ID]['img_after_optm'][$order] = array('size' => 0, 'path' => $img_after, 'src' => $srcs['img_after_url'], 'src_origin' => $srcs['img_before_url'], 'width' => $width, 'height' => $height, 'dimension' => ImageHelper::IGetDimension($img_after));
                 //Get the number of images which their size are not good
                 if (!isset($iNotGoodTotal[$iID])) {
                     $iNotGoodTotal[$iID] = 0;
                 }
                 $iNotGoodTotal[$iID]++;
             } else {
                 if (!isset($iNotGood[$iID])) {
                     $iNotGood[$iID] = array();
                 }
                 if (!isset($iNotGoodTotal[$iID])) {
                     $iNotGoodTotal[$iID] = 0;
                 }
             }
             //Get image that its meta/metas is/are not good
             if (count($meta_checkout) > 0) {
                 foreach ($meta_checkout as $key => $meta) {
                     $meta_value = $img[$meta];
                     $imNotGood[$iID][$post->ID]['ID'] = $post->ID;
                     $imNotGood[$iID][$post->ID]['title'] = $post->post_title;
                     $imNotGood[$iID][$post->ID]['post_type'] = $post->post_type;
                     $imNotGood[$iID][$post->ID]['meta'][$order]['img_src'] = $img['src'];
                     $imNotGood[$iID][$post->ID]['meta'][$order]['type'][$meta] = $meta_value;
                     #if($forceScan !== TRUE && $meta_value != ''){
                     #unset($_posts['meta_not_good'][$img_post_id][$post->ID]['meta'][$order]['type'][$meta]);
                     #}
                     if ($meta_value == '') {
                         if (!isset($imNotGoodTotal[$iID][$meta])) {
                             $imNotGoodTotal[$iID][$meta] = 0;
                         }
                         $imNotGoodTotal[$iID][$meta]++;
                     }
                 }
             }
         }
     }
     foreach ($imgs as $name => $iID) {
         if (!isset($iNotGoodTotal[$iID])) {
             $iNotGoodTotal[$iID] = -1;
         }
         if (!isset($iNotGood[$iID])) {
             $iNotGood[$iID] = array();
         }
         if (!isset($imNotGood[$iID])) {
             $imNotGood[$iID] = array();
         }
         if (!isset($imNotGoodTotal[$iID])) {
             foreach ($meta_checkout as $mkey) {
                 $imNotGoodTotal[$iID][$mkey] = 0;
             }
         }
     }
     foreach ($imNotGoodTotal as &$mStatis) {
         foreach ($meta_checkout as $mkey) {
             if (!isset($mStatis[$mkey])) {
                 $mStatis[$mkey] = 0;
             }
         }
     }
     unset($posts, $imgs);
     $ret = array('iNotGood' => $iNotGood, 'iNotGoodTotal' => $iNotGoodTotal, 'imNotGood' => $imNotGood, 'imNotGoodTotal' => $imNotGoodTotal);
     return $ret;
 }