✏️ 正在编辑: image.php
路径:
/home/qyel0117/public_html/wp-content/themes/semplice7/includes/helper/image.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php // namespace namespace Semplice\Helper; // ----------------------------------------- // semplice image helper // ----------------------------------------- class Image { // ----------------------------------------- // get image // ----------------------------------------- public static function get($id, $size) { // image $image = false; // make sure id is not empty if(!empty($id)) { // is id or url? if(is_numeric($id)) { // get attachment $attachment = wp_get_attachment_image_src($id, $size, false); // is array? if(is_array($attachment)) { // get svg dimensions if((strtolower(substr($attachment[0], -4)) === '.svg')) { $attachment = self::get_svg_dimensions($id, $attachment); } // add image $image = array( 'src' => $attachment[0], 'width' => $attachment[1], 'height' => $attachment[2], 'alt' => self::alt($id), 'title' => get_the_title($id), 'caption'=> wp_get_attachment_caption($id), 'sizes' => array( 'medium' => wp_get_attachment_image_url($id, 'medium'), 'medium_large' => wp_get_attachment_image_url($id, 'medium_large') ), ); } else { $image = array( 'src' => SEMPLICE_URI . '/assets/images/admin/image_missing.svg', 'width' => 500, 'height' => 500, 'alt' => 'Image not found', 'caption' => '', ); } } else { $attachment = self::external($id); // is svg? $image = array( 'src' => $attachment['src'], 'width' => $attachment['width'], 'height' => $attachment['height'], 'alt' => $attachment['alt'], 'caption' => '', ); } } // no sizes? if($image && !isset($image['sizes'])) { $image['sizes'] = array('medium' => 'notfound', 'medium_large' => 'notfound'); } // return return $image; } // ----------------------------------------- // get image src // ----------------------------------------- public static function src($id, $size) { // get image url $image = wp_get_attachment_image_src($id, $size, false); // is still there? if(is_array($image)) { return $image[0]; } else { return 'notfound'; } } // ----------------------------------------- // get images/videos thats being used in a module // ----------------------------------------- public static function used_in_module($content) { // vars $module = $content['module']; $options = $content['options']; $image = false; // get image if($module == 'video') { if(isset($options['poster'])) { $image = $options['poster']; } } else if($module == 'button') { if(isset($options['icon'])) { $image = $options['icon']; } } else if($module == 'beforeafter') { // before $image = array(); if(isset($options['before'])) { $image[] = $options['before']; } if(isset($options['after'])) { $image[] = $options['after']; } } else if($module == 'advancedportfoliogrid') { $image = array(); if(isset($options['posts']) && is_array($options['posts'])) { foreach($options['posts'] as $post_options) { if(isset($post_options['thumbnail']) && is_numeric($post_options['thumbnail'])) { $image[] = $post_options['thumbnail']; } } } } else if(!empty($content['content']['xl'])) { $image = $content['content']['xl']; } // check if gallery or normal image if(is_numeric($image)) { return $image . ','; } else if(is_array($image)) { return implode(',', $image) . ','; } } // ----------------------------------------- // get external image // ----------------------------------------- public static function external($id) { // get media json $media = json_decode(file_get_contents(SEMPLICE_DIR . '/assets/json/media.json'), true); // return image if(strpos($id, 'svg') !== false) { return array( 'src' => self::semplice_image($id), 'width' => 0, 'height' => 0, 'alt' => 'svg-image', ); } else if(strpos($id, 'unsplash') !== false) { // set default image height $height = 1080; // get pos of ratio in url $pos = strpos($id, '#ratio'); // check if ratio is set if ($pos !== false) { // get ratio $ratio = floatval(substr($id, $pos+strlen('#ratio:'))); // set image height $height = round(1080 / $ratio, 0); // remove ratio from id $id = substr($id, 0, strpos($id, "#ratio")); } return array( 'src' => $id, 'width' => '1080', 'height' => round(1080 / $ratio, 2), 'alt' => 'unsplash-image', ); } else if(strpos($id, 'semplice') !== false) { return array( 'src' => self::semplice_image($id), 'width' => $media['images'][$id]['width'], 'height' => $media['images'][$id]['height'], 'alt' => 'semplice-blocks-image', ); } else { return array( 'src' => SEMPLICE_URI . '/assets/images/admin/image_missing.svg', 'width' => 500, 'height' => 500, 'alt' => 'Image not found', 'caption' => '' ); } } // ----------------------------------------- // semplice image // ----------------------------------------- public static function semplice_image($id) { // get version $version = (strpos($id, 'semplice7') !== false) ? 'v7' : 'v5'; $replace = (strpos($id, 'semplice7') !== false) ? 'semplice7_' : 'semplice_'; // return return 'https://blocks.semplice.com/' . $version . '/images/' . str_replace($replace, '', $id); } // ----------------------------------------- // image alt // ----------------------------------------- public static function alt($id) { $alt = get_post_meta($id, '_wp_attachment_image_alt', true); // check if alt text is available if(empty($alt)) { $alt = get_the_title($id); } // return return $alt; } // ----------------------------------------- // blocks image array // ----------------------------------------- public static function blocks_array($images) { $output = array(); // check if images array is empty? if(!empty(array_filter($images))) { // fetch all image urls in case they have chnaged (ex domain) foreach ($images as $id) { // video thumbnail if(is_numeric($id)) { $mime = get_post_mime_type($id); if($mime && strpos($mime, 'video') === 0) { $output[$id] = array( 'src' => wp_get_attachment_url($id), 'mime' => $mime, ); continue; } } // get image $output[$id] = self::get($id, 'full'); } } // return return $output; } // ---------------------------------------- // get admin images // ---------------------------------------- public static function admin_images() { // vars $ids = json_decode(get_option('semplice_admin_images'), true); $images = array(); // iterate through images if(is_array($ids) && !empty($ids)) { // check if broken or empty array if(array_key_exists (0, $ids) && null === $ids[0]) { $images = new stdClass(); } else { // fetch all image urls in case they have chnaged (ex domain) foreach ($ids as $id => $url) { $image = self::get($id, 'full'); // is array? if(is_array($image)) { // add image $images[$id] = $image; } } } } return $images; } // ---------------------------------------- // get svg dimensions // ---------------------------------------- public static function get_svg_dimensions($id, $attachment) { $file = get_attached_file($id); if(!$file || !is_readable($file)) { return $attachment;; } $dom = new \DOMDocument(); if(!@$dom->loadXML(file_get_contents($file), LIBXML_NONET)) { return $attachment;; } $svg = $dom->getElementsByTagName('svg')->item(0); if(!$svg) { return $attachment;; } $w = self::svg_px($svg->getAttribute('width')); $h = self::svg_px($svg->getAttribute('height')); if((!$w || !$h) && preg_match('/[\d.]+\s*,?\s*[\d.]+\s*,?\s*([\d.]+)\s*,?\s*([\d.]+)/', $svg->getAttribute('viewBox'), $m)) { $vw = (float) $m[1]; $vh = (float) $m[2]; $w = $w ?: ($h && $vh ? $h * $vw / $vh : $vw); $h = $h ?: ($w && $vw ? $w * $vh / $vw : $vh); } if($w && $h) { $attachment[1] = (int) round($w); $attachment[2] = (int) round($h); } return $attachment; } // ---------------------------------------- // svg px // ---------------------------------------- public static function svg_px($v) { if (!preg_match('/^([\d.]+)\s*([a-z]*)$/i', trim($v), $m) || str_contains($v, '%')) { return null; } $n = (float) $m[1]; return match(strtolower($m[2])) { '', 'px' => $n, 'in' => $n * 96, 'cm' => $n * 37.8, 'mm' => $n * 3.78, 'pt' => $n * 1.33, 'pc' => $n * 16, default => $n }; } } new Image; ?>
💾 保存文件
← 返回文件管理器