| 1 |
<?php |
| 2 |
/** |
| 3 |
* Routines for generation of custom image sizes and deletion of these sizes. |
| 4 |
* |
| 5 |
* @since 1.9.0 |
| 6 |
* @package themify |
| 7 |
*/ |
| 8 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 9 |
|
| 10 |
if ( ! function_exists( 'themify_do_img' ) ) { |
| 11 |
/** |
| 12 |
* Resize images dynamically using wp built in functions |
| 13 |
* |
| 14 |
* @param string|int $image Image URL or an attachment ID |
| 15 |
* @param int $width |
| 16 |
* @param int $height |
| 17 |
* @param bool $crop |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function themify_do_img( $image, $width, $height,bool $crop = false ):array { |
| 21 |
$attachment_id =$img_url= null; |
| 22 |
if(!is_numeric( $width ) ){ |
| 23 |
$width=''; |
| 24 |
} |
| 25 |
if(!is_numeric( $height ) ){ |
| 26 |
$height=''; |
| 27 |
} |
| 28 |
// if an attachment ID has been sent |
| 29 |
if( is_numeric( $image ) ) { |
| 30 |
$post = get_post( $image ); |
| 31 |
if( $post ) { |
| 32 |
$attachment_id = $post->ID; |
| 33 |
$img_url = wp_get_attachment_url( $attachment_id ); |
| 34 |
} |
| 35 |
unset($post); |
| 36 |
} else { |
| 37 |
if(strpos($image,'data:image/' )!==false ){ |
| 38 |
return array( |
| 39 |
'url' =>$image, |
| 40 |
'width' => $width, |
| 41 |
'height' => $height |
| 42 |
); |
| 43 |
} |
| 44 |
// URL has been passed to the function |
| 45 |
$img_url = esc_url( $image ); |
| 46 |
|
| 47 |
// Check if the image is an attachment. If it's external return url, width and height. |
| 48 |
if(strpos($img_url,themify_upload_dir('baseurl'))===false){ |
| 49 |
if($width==='' || $height===''){ |
| 50 |
$size = themify_get_image_size($img_url); |
| 51 |
if($size!==false){ |
| 52 |
if($width===''){ |
| 53 |
$width=$size['w']; |
| 54 |
} |
| 55 |
if($height===''){ |
| 56 |
$height=$size['h']; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return array( |
| 61 |
'url' =>$img_url, |
| 62 |
'width' => $width, |
| 63 |
'height' => $height |
| 64 |
); |
| 65 |
} |
| 66 |
// Finally, run a custom database query to get the attachment ID from the modified attachment URL |
| 67 |
$attachment_id = themify_get_attachment_id_from_url( $img_url); |
| 68 |
} |
| 69 |
// Fetch attachment metadata. Up to this point we know the attachment ID is valid. |
| 70 |
$meta = $attachment_id ?wp_get_attachment_metadata( $attachment_id ):null; |
| 71 |
|
| 72 |
// missing metadata. bail. |
| 73 |
if (!is_array( $meta )) { |
| 74 |
if($img_url!==null){ |
| 75 |
$ext=strtolower(strtok(pathinfo($img_url,PATHINFO_EXTENSION ),'?')); |
| 76 |
if($ext==='png' || $ext==='jpg' || $ext==='jpeg' || $ext==='webp' || $ext==='gif' ||$ext==='bmp' ){//popular types |
| 77 |
$upload_dir = themify_upload_dir(); |
| 78 |
$attached_file=str_replace($upload_dir['baseurl'],$upload_dir['basedir'],$img_url); |
| 79 |
if(!is_file ($attached_file)){ |
| 80 |
$attached_file=$attachment_id?get_attached_file( $attachment_id ):null; |
| 81 |
} |
| 82 |
if($attached_file){ |
| 83 |
$size=themify_get_image_size($attached_file,true); |
| 84 |
if($size){ |
| 85 |
$meta=array( |
| 86 |
'width'=>$size['w'], |
| 87 |
'height'=>$size['h'], |
| 88 |
'file'=>trim(str_replace($upload_dir['basedir'],'',$attached_file),'/') |
| 89 |
); |
| 90 |
//if the meta doesn't exist it means the image large size also doesn't exist,that is why checking if the image is too large before cropping,otherwise the site will down |
| 91 |
if($meta['width']>2560 || $meta['height']>2560){ |
| 92 |
return array( |
| 93 |
'url' => $img_url, |
| 94 |
'width' => $width, |
| 95 |
'height' => $height, |
| 96 |
'is_large'=>true |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
unset($upload_dir,$ext,$size,$attached_file); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
if ( ! is_array( $meta ) ) { |
| 106 |
return array( |
| 107 |
'url' => $img_url, |
| 108 |
'width' => $width, |
| 109 |
'height' => $height |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Perform calculations when height or width = 0 |
| 115 |
if( empty( $width ) ) { |
| 116 |
$width = 0; |
| 117 |
} |
| 118 |
if ( empty( $height ) ) { |
| 119 |
// If width and height or original image are available as metadata |
| 120 |
if ( !empty( $meta['width'] ) && !empty( $meta['height'] ) ) { |
| 121 |
// Divide width by original image aspect ratio to obtain projected height |
| 122 |
// The floor function is used so it returns an int and metadata can be written |
| 123 |
$height = (int)(floor( $width / ( $meta['width'] / $meta['height'] ) )); |
| 124 |
} else { |
| 125 |
$height = 0; |
| 126 |
} |
| 127 |
} |
| 128 |
// Check if resized image already exists |
| 129 |
if ( is_array( $meta ) && isset( $meta['sizes']["resized-{$width}x{$height}"] ) ) { |
| 130 |
$size = $meta['sizes']["resized-{$width}x{$height}"]; |
| 131 |
if( isset( $size['width'],$size['height'] )) { |
| 132 |
$split_url = explode( '/', $img_url ); |
| 133 |
|
| 134 |
if( ! isset( $size['mime-type'] ) || $size['mime-type'] !== 'image/gif' ) { |
| 135 |
$split_url[ count( $split_url ) - 1 ] = $size['file']; |
| 136 |
} |
| 137 |
|
| 138 |
return array( |
| 139 |
'url' => implode( '/', $split_url ), |
| 140 |
'width' => $width, |
| 141 |
'height' => $height, |
| 142 |
'attachment_id' => $attachment_id |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// Requested image size doesn't exists, so let's create one |
| 148 |
if ( true === $crop ) { |
| 149 |
add_filter( 'image_resize_dimensions', 'themify_img_resize_dimensions', 10, 5 ); |
| 150 |
} |
| 151 |
// Patch meta because if we're here, there's a valid attachment ID for sure, but maybe the metadata is not ok. |
| 152 |
if ( empty( $meta ) ) { |
| 153 |
$meta['sizes'] = array( 'large' => array() ); |
| 154 |
} |
| 155 |
// Generate image returning an array with image url, width and height. If image can't be generated, original url, width and height are used. |
| 156 |
$image = themify_make_image_size( $attachment_id, $width, $height, $meta, $img_url ); |
| 157 |
|
| 158 |
if ( true === $crop ) { |
| 159 |
remove_filter( 'image_resize_dimensions', 'themify_img_resize_dimensions', 10 ); |
| 160 |
} |
| 161 |
$image['attachment_id'] = $attachment_id; |
| 162 |
return $image; |
| 163 |
} |
| 164 |
} |
| 165 |
if ( ! function_exists( 'themify_make_image_size' ) ) { |
| 166 |
/** |
| 167 |
* Creates new image size. |
| 168 |
* |
| 169 |
* @uses get_attached_file() |
| 170 |
* @uses image_make_intermediate_size() |
| 171 |
* @uses wp_update_attachment_metadata() |
| 172 |
* @uses get_post_meta() |
| 173 |
* @uses update_post_meta() |
| 174 |
* |
| 175 |
* @param int $attachment_id |
| 176 |
* @param int $width |
| 177 |
* @param int $height |
| 178 |
* @param array $meta |
| 179 |
* @param string $img_url |
| 180 |
* |
| 181 |
* @return array |
| 182 |
*/ |
| 183 |
function themify_make_image_size( $attachment_id, $width, $height, $meta, $img_url ):array { |
| 184 |
if($width!==0 || $height!==0){ |
| 185 |
$upload_dir = themify_upload_dir(); |
| 186 |
$attached_file=str_replace($upload_dir['baseurl'],$upload_dir['basedir'],$img_url); |
| 187 |
unset($upload_dir); |
| 188 |
if(!Themify_Filesystem::is_file ($attached_file)){ |
| 189 |
$attached_file=get_attached_file( $attachment_id ); |
| 190 |
} |
| 191 |
$source_size = apply_filters( 'themify_image_script_source_size', themify_get( 'setting-img_php_base_size', 'large', true ) ); |
| 192 |
if ( $source_size !== 'full' && isset( $meta['sizes'][ $source_size ]['file'] ) ){ |
| 193 |
$attached_file = str_replace( $meta['file'], trailingslashit( dirname( $meta['file'] ) ) . $meta['sizes'][ $source_size ]['file'], $attached_file ); |
| 194 |
} |
| 195 |
unset($source_size); |
| 196 |
$resized = image_make_intermediate_size( $attached_file, $width, $height, true ); |
| 197 |
if ( $resized && ! is_wp_error( $resized ) ) { |
| 198 |
// Save the new size in metadata |
| 199 |
$key = sprintf( 'resized-%dx%d', $width, $height ); |
| 200 |
$meta['sizes'][$key] = $resized; |
| 201 |
$img_url = str_replace( basename( $img_url ), $resized['file'], $img_url ); |
| 202 |
|
| 203 |
wp_update_attachment_metadata( $attachment_id, $meta ); |
| 204 |
// Save size in backup sizes, so it's deleted when the original attachment is deleted. |
| 205 |
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); |
| 206 |
if ( ! is_array( $backup_sizes ) ){ |
| 207 |
$backup_sizes = array(); |
| 208 |
} |
| 209 |
$backup_sizes[$key] = $resized; |
| 210 |
update_post_meta( $attachment_id, '_wp_attachment_backup_sizes', $backup_sizes ); |
| 211 |
$img_url=esc_url($img_url); |
| 212 |
} |
| 213 |
} |
| 214 |
// Return original image url, width and height. |
| 215 |
return array( |
| 216 |
'url' => $img_url, |
| 217 |
'width' => $width, |
| 218 |
'height' => $height |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* Disable the min commands to choose the minimum dimension, thus enabling image enlarging. |
| 227 |
* |
| 228 |
* @param $default |
| 229 |
* @param $orig_w |
| 230 |
* @param $orig_h |
| 231 |
* @param $dest_w |
| 232 |
* @param $dest_h |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
function themify_img_resize_dimensions( $default, $orig_w, $orig_h, $dest_w, $dest_h ):array { |
| 236 |
// set portion of the original image that we can size to $dest_w x $dest_h |
| 237 |
$aspect_ratio = $orig_w / $orig_h; |
| 238 |
$new_w = $dest_w; |
| 239 |
$new_h = $dest_h; |
| 240 |
|
| 241 |
if ( !$new_w ) { |
| 242 |
$new_w = (int)( $new_h * $aspect_ratio ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( !$new_h ) { |
| 246 |
$new_h = (int)( $new_w / $aspect_ratio ); |
| 247 |
} |
| 248 |
|
| 249 |
$size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); |
| 250 |
|
| 251 |
$crop_w = round( $new_w / $size_ratio ); |
| 252 |
$crop_h = round( $new_h / $size_ratio ); |
| 253 |
|
| 254 |
$s_x = floor( ( $orig_w - $crop_w ) / 2 ); |
| 255 |
$s_y = floor( ( $orig_h - $crop_h ) / 2 ); |
| 256 |
|
| 257 |
// the return array matches the parameters to imagecopyresampled() |
| 258 |
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h |
| 259 |
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); |
| 260 |
} |
| 261 |
|
| 262 |
if( ! function_exists( 'themify_get_attachment_id_from_url' ) ) : |
| 263 |
/** |
| 264 |
* Get attachment ID for image from its url. |
| 265 |
* @param deprecated $base_url |
| 266 |
*/ |
| 267 |
function themify_get_attachment_id_from_url(string $url = '', $base_url = '' ):int { |
| 268 |
/* cache IDs, for when an image is displayed multiple times on the same page */ |
| 269 |
static $cache = array(); |
| 270 |
|
| 271 |
// If this is the URL of an auto-generated thumbnail, get the URL of the original image |
| 272 |
$url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif|webp|bmp)$)/i', '', $url ); |
| 273 |
if ( ! empty( $url ) ) { |
| 274 |
if ( ! isset( $cache[ $url ] ) ) { |
| 275 |
$attachment_id = themify_get_attachment_id_cache( $url ); |
| 276 |
$attachment_id = themify_maybe_translate_object_id( $attachment_id, 'post' ); |
| 277 |
$cache[ $url ] = $attachment_id; |
| 278 |
} |
| 279 |
return $cache[ $url ]; |
| 280 |
} |
| 281 |
return 0; |
| 282 |
} |
| 283 |
endif; |
| 284 |
|
| 285 |
/** |
| 286 |
* Convert image URL to attachment ID, data is cached in a db for faster access |
| 287 |
*/ |
| 288 |
function themify_get_attachment_id_cache(string $url ):int { |
| 289 |
$k=$url.'_id'; |
| 290 |
$id = Themify_Storage::get($k); |
| 291 |
if ($id==='0' || ($id>0 && get_post_type($id)==='attachment') ) { |
| 292 |
return (int) $id; |
| 293 |
} |
| 294 |
$id = attachment_url_to_postid( $url ); |
| 295 |
Themify_Storage::set($k,$id); |
| 296 |
return $id; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* Removes protocol and www from URL and returns it |
| 302 |
* |
| 303 |
* @return string |
| 304 |
*/ |
| 305 |
function themify_remove_protocol_from_url( $url ) {//deprecated will be removed |
| 306 |
return preg_replace( '/https?:\/\/(www\.)?/', '', $url ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
function themify_create_webp(string $url):string{//@todo move to class |
| 311 |
|
| 312 |
$res=$url; |
| 313 |
$info = pathinfo($res); |
| 314 |
if(!isset($info['extension'])){ |
| 315 |
return $url; |
| 316 |
} |
| 317 |
$orig_ex = strtok($info['extension'],'?'); |
| 318 |
if($orig_ex!=='png' && $orig_ex!=='jpg' && $orig_ex!=='jpeg' && $orig_ex!=='gif'){ |
| 319 |
return $url; |
| 320 |
} |
| 321 |
static $available=null; |
| 322 |
if($available===NULL){ |
| 323 |
$available=array(); |
| 324 |
if(apply_filters('themify_disable_webp',false)===false){ |
| 325 |
if(class_exists('Imagick',false)){ |
| 326 |
$im = new Imagick(); |
| 327 |
if (in_array('WEBP', $im->queryFormats('WEBP'),true) ) { |
| 328 |
$available['Imagick']=true; |
| 329 |
} |
| 330 |
$im->clear(); |
| 331 |
$im=null; |
| 332 |
} |
| 333 |
if(!isset($available['Imagick']) &&function_exists('imagewebp') && (function_exists('imagecreatefromjpeg') || function_exists('imagecreatefrompng'))){ |
| 334 |
$available['GD']=true; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
if(!empty($available)){ |
| 339 |
$upload_dir= themify_upload_dir(); |
| 340 |
$sameDomain=strpos($url,$upload_dir['baseurl'])!==false; |
| 341 |
if($sameDomain===false && strpos($url,'http')!==0){//relative to absolute |
| 342 |
$tmp_url = home_url($url); |
| 343 |
$sameDomain=strpos($tmp_url,$upload_dir['baseurl'])!==false; |
| 344 |
if($sameDomain===true){ |
| 345 |
$res=$tmp_url; |
| 346 |
} |
| 347 |
} |
| 348 |
if(is_multisite()){ |
| 349 |
if($sameDomain===false){ |
| 350 |
if(is_subdomain_install()){ |
| 351 |
$blog_name = explode('.',$_SERVER['SERVER_NAME']); |
| 352 |
$blog_name=$blog_name[0]; |
| 353 |
if(strpos($url,$blog_name)===false){ |
| 354 |
return $url; |
| 355 |
} |
| 356 |
} |
| 357 |
else{ |
| 358 |
if(!isset($_SERVER['SERVER_NAME']) || strpos($url,$_SERVER['SERVER_NAME'])===false){ |
| 359 |
return $url; |
| 360 |
} |
| 361 |
static $site_url=null; |
| 362 |
if($site_url===null){ |
| 363 |
$site_url = dirname(site_url()); |
| 364 |
} |
| 365 |
if(strpos($url,$site_url)===false){ |
| 366 |
return $url; |
| 367 |
} |
| 368 |
$blog_name =explode('/',trim(str_replace($site_url,'',$url),'/')); |
| 369 |
$blog_name=$blog_name[0]; |
| 370 |
} |
| 371 |
static $sites=array(); |
| 372 |
if(!isset($sites[$blog_name])){ |
| 373 |
$blog = get_id_from_blogname($blog_name); |
| 374 |
if($blog===null){ |
| 375 |
$sites[$blog_name]=false; |
| 376 |
return $url; |
| 377 |
} |
| 378 |
$currentBlog=pathinfo(get_site_url(),PATHINFO_FILENAME); |
| 379 |
switch_to_blog($blog ); |
| 380 |
|
| 381 |
$blog_upload_dir_info = wp_get_upload_dir(); |
| 382 |
restore_current_blog(); |
| 383 |
$sites[$blog_name] = array('basedir'=>$blog_upload_dir_info['basedir'],'baseurl'=>str_replace('/'.$currentBlog.'/','/'.$blog_name.'/',$blog_upload_dir_info['baseurl']));// bug in WP return the current blog name url,not switched |
| 384 |
} |
| 385 |
elseif($sites[$blog_name]===false){ |
| 386 |
return $url; |
| 387 |
} |
| 388 |
$upload_dir=$sites[$blog_name]; |
| 389 |
} |
| 390 |
} |
| 391 |
elseif($sameDomain===false){ |
| 392 |
return $url; |
| 393 |
} |
| 394 |
$res=str_replace($upload_dir['baseurl'],$upload_dir['basedir'],urldecode($res)); |
| 395 |
if(strpos($res,'http')===0){ |
| 396 |
return $url; |
| 397 |
} |
| 398 |
$resUrl=str_replace('.'.$orig_ex, '.webp', $res); |
| 399 |
if(is_file ($resUrl)){ |
| 400 |
return str_replace($upload_dir['basedir'],$upload_dir['baseurl'],$resUrl); |
| 401 |
} |
| 402 |
if ( ! is_file( $res ) || ! is_readable( $res ) ){ |
| 403 |
return $url; |
| 404 |
} |
| 405 |
// Check dimensions before decoding — Imagick/GD full load can OOM/timeout on large images |
| 406 |
$imgInfo = @getimagesize( $res ); |
| 407 |
if ( empty( $imgInfo[0] ) || empty( $imgInfo[1] ) || empty( $imgInfo['mime'] ) ) { |
| 408 |
return $url; |
| 409 |
} |
| 410 |
if ( $imgInfo[0] > 2560 || $imgInfo[1] > 2560 ) { |
| 411 |
return $url; |
| 412 |
} |
| 413 |
$webp_quality = (int) themify_builder_get( 'setting-webp-quality', 'performance-webp_quality' ); |
| 414 |
if ( empty( $webp_quality ) ) { |
| 415 |
$webp_quality = 5; |
| 416 |
} |
| 417 |
if(isset($available['Imagick'])){ |
| 418 |
$im = null; |
| 419 |
try { |
| 420 |
$im = new Imagick($res); |
| 421 |
$lowerExt=explode('/',$im->getImageMimeType()); |
| 422 |
if(isset($lowerExt[1])){ |
| 423 |
$lowerExt=str_replace('x-','',$lowerExt[1]); |
| 424 |
}else{ |
| 425 |
$lowerExt=false; |
| 426 |
} |
| 427 |
if($lowerExt!=='png' && $lowerExt!=='jpg' && $lowerExt!=='jpeg' && $lowerExt!=='gif'){ |
| 428 |
$im->clear(); |
| 429 |
$im=null; |
| 430 |
return $url; |
| 431 |
} |
| 432 |
if($im->setImageFormat( 'webp' ) && $im->setOption( 'webp:method', $webp_quality ) && $im->setOption('webp:lossless','false') && $im->setOption('webp:low-memory', 'true') && $im->setOption('webp:use-sharp-yuv', 'true')) { |
| 433 |
|
| 434 |
if (($lowerExt !== 'png' || ($im->setOption('webp:alpha-compression', 1) && $im->setOption('webp:alpha-quality', 85))) && $im->stripImage()) { |
| 435 |
|
| 436 |
try { |
| 437 |
$webp = $lowerExt === 'gif' ? $im->writeImages($resUrl, true) : $im->writeImage($resUrl); |
| 438 |
} |
| 439 |
catch (Throwable $e ){ |
| 440 |
$webp=false; |
| 441 |
} |
| 442 |
if(!$webp){ |
| 443 |
if($lowerExt === 'gif') { |
| 444 |
try { |
| 445 |
$im->optimizeImageLayers(); |
| 446 |
} |
| 447 |
catch (Throwable $e) { |
| 448 |
|
| 449 |
} |
| 450 |
} |
| 451 |
$webp = file_put_contents($resUrl, ($lowerExt==='gif'?$im->getImagesBlob():$im->getImageBlob())); |
| 452 |
if ($webp) { |
| 453 |
$res = $resUrl; |
| 454 |
}else{ |
| 455 |
Themify_Filesystem::delete($resUrl,'f'); |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
catch (Throwable $e ){ |
| 462 |
if ( $im instanceof Imagick ) { |
| 463 |
$im->clear(); |
| 464 |
} |
| 465 |
$im=null; |
| 466 |
return $url; |
| 467 |
} |
| 468 |
if ( $im instanceof Imagick ) { |
| 469 |
$im->clear(); |
| 470 |
$im = null; |
| 471 |
} |
| 472 |
} |
| 473 |
else{ |
| 474 |
$size = $imgInfo['mime']; |
| 475 |
$size=explode('/',$size); |
| 476 |
if(!isset($size[1])){ |
| 477 |
return $url; |
| 478 |
} |
| 479 |
$lowerExt=$size[1]; |
| 480 |
unset($size); |
| 481 |
if($lowerExt!=='png' && $lowerExt!=='jpg' && $lowerExt!=='jpeg'){ |
| 482 |
return $url; |
| 483 |
} |
| 484 |
|
| 485 |
$im = null; |
| 486 |
try { |
| 487 |
switch($lowerExt){ |
| 488 |
case 'jpeg': |
| 489 |
case 'jpg': |
| 490 |
if(!function_exists('imagecreatefromjpeg')){ |
| 491 |
return $url; |
| 492 |
} |
| 493 |
$im = @imagecreatefromjpeg($res); |
| 494 |
break; |
| 495 |
case 'png': |
| 496 |
if(!function_exists('imagecreatefrompng')){ |
| 497 |
return $url; |
| 498 |
} |
| 499 |
if(function_exists('imagepalettetotruecolor')){ |
| 500 |
$im = @imagecreatefrompng($res); |
| 501 |
if($im!==false && (!imagepalettetotruecolor($im) || !imagealphablending($im, true) || !imagesavealpha($im, true))){ |
| 502 |
imagedestroy($im); |
| 503 |
$im=null; |
| 504 |
} |
| 505 |
} |
| 506 |
else{ |
| 507 |
$pngimg = @imagecreatefrompng($res); |
| 508 |
if($pngimg!==false) { |
| 509 |
$w = imagesx($pngimg); |
| 510 |
$h = imagesy($pngimg); |
| 511 |
if ($w !== false && $h !== false) { |
| 512 |
$im = imagecreatetruecolor($w, $h); |
| 513 |
if ($im !== false && imagealphablending($im, false) && imagesavealpha($im, true)) { |
| 514 |
$trans = imagecolorallocatealpha($im, 0, 0, 0, 127); |
| 515 |
if($trans===false || !imagefilledrectangle($im, 0, 0, $w - 1, $h - 1, $trans) || !imagecopy($im, $pngimg, 0, 0, 0, 0, $w, $h)) { |
| 516 |
imagedestroy($im); |
| 517 |
$im=null; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
imagedestroy($pngimg); |
| 522 |
} |
| 523 |
$pngimg=null; |
| 524 |
} |
| 525 |
break; |
| 526 |
default: |
| 527 |
return $url; |
| 528 |
} |
| 529 |
|
| 530 |
if(empty($im)){ |
| 531 |
return $url; |
| 532 |
} |
| 533 |
$res=$resUrl; |
| 534 |
$quality = array( 0 => 40, 1 => 50, 2 => 60, 3 => 70, 4 => 80, 5 => 90, 6 => 100 ); |
| 535 |
$webp =@imagewebp($im, $res, $quality[ $webp_quality ] ); |
| 536 |
if(!$webp){ |
| 537 |
Themify_Filesystem::delete($res,'f'); |
| 538 |
} |
| 539 |
} |
| 540 |
catch (Throwable $e) { |
| 541 |
if ( ! empty( $im ) ) { |
| 542 |
imagedestroy($im); |
| 543 |
} |
| 544 |
$im=null; |
| 545 |
return $url; |
| 546 |
} |
| 547 |
if ( ! empty( $im ) ) { |
| 548 |
imagedestroy($im); |
| 549 |
} |
| 550 |
$im=null; |
| 551 |
} |
| 552 |
return !empty($webp)?str_replace($upload_dir['basedir'],$upload_dir['baseurl'],$res):$url; |
| 553 |
} |
| 554 |
else{ |
| 555 |
return $url; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
function themify_get_video_size(string $url):?array{ |
| 560 |
$k=$url.'_size'; |
| 561 |
$found=Themify_Storage::get($k); |
| 562 |
if($found===false){ |
| 563 |
$found=null; |
| 564 |
$attachment_id=themify_get_attachment_id_from_url($url); |
| 565 |
if($attachment_id>0){ |
| 566 |
$meta=wp_get_attachment_metadata( $attachment_id ); |
| 567 |
if(empty($meta)){ |
| 568 |
require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 569 |
$meta=wp_read_video_metadata(get_attached_file($attachment_id)); |
| 570 |
} |
| 571 |
if(!empty($meta)){ |
| 572 |
$found=array( |
| 573 |
'w'=>isset($meta['width'])?$meta['width']:'', |
| 574 |
'h'=>isset($meta['height'])?$meta['height']:'', |
| 575 |
's'=>isset($meta['filesize'])?$meta['filesize']:'', |
| 576 |
'f'=>isset($meta['fileformat'])?$meta['fileformat']:'', |
| 577 |
'l'=>isset($meta['length_formatted'])?$meta['length_formatted']:'', |
| 578 |
't'=>isset($meta['mime_type'])?$meta['mime_type']:'' |
| 579 |
); |
| 580 |
Themify_Storage::set($k,$found,MONTH_IN_SECONDS*6); |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
else{ |
| 585 |
$found=json_decode($found,true); |
| 586 |
if($found===null && Themify_Storage::delete($k)!==false){ |
| 587 |
return themify_get_video_size($url); |
| 588 |
} |
| 589 |
} |
| 590 |
return $found; |
| 591 |
} |
| 592 |
|
| 593 |
function themify_get_image_size(?string $url,bool $isLocal=false,int $color=0){//@todo move to class |
| 594 |
if(!isset($url[2]) || strtok(pathinfo($url,PATHINFO_EXTENSION),'?')==='svg'){ |
| 595 |
return false; |
| 596 |
} |
| 597 |
if(strpos($url,'x',3)!==false){ |
| 598 |
preg_match('/\-(\d+x\d+)\./i',$url,$m,0,3); |
| 599 |
if(isset($m[1])){ |
| 600 |
$m=explode('x',$m[1]); |
| 601 |
$size= array('w'=>$m[0],'h'=>$m[1]); |
| 602 |
if($color===0){ |
| 603 |
return $size; |
| 604 |
} |
| 605 |
} |
| 606 |
unset($m); |
| 607 |
} |
| 608 |
elseif(strpos($url,'gravatar.com')!==false){ |
| 609 |
$parts = parse_url($url,PHP_URL_QUERY); |
| 610 |
if(!empty($parts)){ |
| 611 |
parse_str($parts, $query_params); |
| 612 |
if(!empty($query_params['s'])){ |
| 613 |
return array('w'=>$query_params['s'],'h'=>$query_params['s']); |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
$k=$url.'_size'; |
| 618 |
if(!isset($size) || $color>0){ |
| 619 |
$found=Themify_Storage::get($k); |
| 620 |
if($found!==''){ |
| 621 |
if(strpos($found,':')!==false){ |
| 622 |
$found=explode(':',$found)[1]; |
| 623 |
} |
| 624 |
$found=explode('-',$found); |
| 625 |
if(isset($found[1])){ |
| 626 |
$size= array('w'=>$found[0],'h'=>$found[1]); |
| 627 |
if($color===0){ |
| 628 |
return $size; |
| 629 |
} |
| 630 |
if(isset($found[2])){ |
| 631 |
$size['c']=explode(',',$found[2]); |
| 632 |
return $size; |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
$url= urldecode($url); |
| 638 |
if($isLocal===false){ |
| 639 |
if(defined('THEME_URI') && strpos($url,THEME_URI)!==false){ |
| 640 |
$url=str_replace(THEME_URI,THEME_DIR,$url); |
| 641 |
$isLocal=true; |
| 642 |
} |
| 643 |
else{ |
| 644 |
$upload_dir = themify_upload_dir(); |
| 645 |
if(strpos($url,$upload_dir['baseurl'])!==false){ |
| 646 |
$isLocal=true; |
| 647 |
$url=str_replace($upload_dir['baseurl'],$upload_dir['basedir'],$url); |
| 648 |
} |
| 649 |
unset($upload_dir); |
| 650 |
} |
| 651 |
} |
| 652 |
if(empty($size)){ |
| 653 |
if (class_exists('Themify_Get_Image_Size',false) || is_file( THEMIFY_DIR . '/class-themify-get-image-size.php' ) ) { |
| 654 |
if($isLocal===false) { |
| 655 |
static $is = null; |
| 656 |
if ($is === null) { |
| 657 |
$is = apply_filters('tf_disable_remote_size', true); |
| 658 |
} |
| 659 |
if ($is === false) { |
| 660 |
return false; |
| 661 |
} |
| 662 |
} |
| 663 |
require_once THEMIFY_DIR . '/class-themify-get-image-size.php'; |
| 664 |
$size=Themify_Get_Image_Size::getSize($url,$isLocal); |
| 665 |
} |
| 666 |
else{ |
| 667 |
$size=false; |
| 668 |
} |
| 669 |
if($size===false && $isLocal===true && function_exists('getimagesize')){ |
| 670 |
$size=getimagesize($url); |
| 671 |
$size=empty($size)?false:array('w'=>$size[0],'h'=>$size[1]); |
| 672 |
} |
| 673 |
} |
| 674 |
if($size!==false){ |
| 675 |
$value=$size['w'].'-'.$size['h']; |
| 676 |
if($color>0 && $isLocal===true ){ |
| 677 |
$colors=themify_get_image_color($url,$size['w'],$size['h'],$color); |
| 678 |
|
| 679 |
if(!empty($colors)){ |
| 680 |
$size['c']=$colors; |
| 681 |
$value.='-'.implode(',',$colors); |
| 682 |
} |
| 683 |
} |
| 684 |
Themify_Storage::set($k,$value); |
| 685 |
} |
| 686 |
return $size; |
| 687 |
} |
| 688 |
|
| 689 |
function themify_get_image_color(string $dir,int $w,int $h,int $rows=4):array{//@todo move to class |
| 690 |
$colors=[]; |
| 691 |
if($w<=2560 || $h<=2560){ |
| 692 |
$isImagIck=null; |
| 693 |
if(class_exists('Imagick',false)){ |
| 694 |
try { |
| 695 |
$im = new Imagick($dir); |
| 696 |
$isImagIck=true; |
| 697 |
} |
| 698 |
catch(Throwable $e) { |
| 699 |
if(isset($im)){ |
| 700 |
$im->clear(); |
| 701 |
$im=null; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
if(!isset($im)){ |
| 707 |
$ext=strtok(pathinfo($dir,PATHINFO_EXTENSION),'?'); |
| 708 |
if ( $ext === 'png' ) { |
| 709 |
if(function_exists('imagecreatefrompng')){ |
| 710 |
$im = imagecreatefrompng($dir); |
| 711 |
} |
| 712 |
} |
| 713 |
elseif($ext==='jpg' || $ext==='jpeg'){ |
| 714 |
if(function_exists('imagecreatefromjpeg')){ |
| 715 |
$im = imagecreatefromjpeg($dir); |
| 716 |
} |
| 717 |
} |
| 718 |
elseif($ext==='gif'){ |
| 719 |
if(function_exists('imagecreatefromgif')){ |
| 720 |
$im = imagecreatefromgif($dir); |
| 721 |
} |
| 722 |
} |
| 723 |
elseif($ext==='webp'){ |
| 724 |
if(function_exists('imagecreatefromwebp')){ |
| 725 |
$im = imagecreatefromwebp($dir); |
| 726 |
} |
| 727 |
} |
| 728 |
elseif($ext==='bmp' && function_exists('imagecreatefrombmp')){ |
| 729 |
$im = imagecreatefrombmp($dir); |
| 730 |
} |
| 731 |
unset($ext); |
| 732 |
} |
| 733 |
if(!empty($im)){ |
| 734 |
$box_w=floor($w/$rows); |
| 735 |
$box_h=floor($h/$rows); |
| 736 |
$half_w=(int)($box_w/2); |
| 737 |
$half_h=(int)($box_h/2); |
| 738 |
for($y=1;$y<=$rows;++$y){ |
| 739 |
$y_coord=$y*$box_h-$half_h; |
| 740 |
for($x=1;$x<=$rows;++$x){ |
| 741 |
$x_coord=$x*$box_w-$half_w; |
| 742 |
if($isImagIck===true){ |
| 743 |
try { |
| 744 |
$tmp=$im->getImagePixelColor($x_coord,$y_coord)->getColor(0); |
| 745 |
$color=array('red'=>$tmp['r'],'green'=>$tmp['g'],'blue'=>$tmp['b']); |
| 746 |
} |
| 747 |
catch(Throwable $e) { |
| 748 |
|
| 749 |
} |
| 750 |
} |
| 751 |
else{ |
| 752 |
$color=imagecolorsforindex($im, imagecolorat($im, $x_coord,$y_coord)); |
| 753 |
} |
| 754 |
if(isset($color)){ |
| 755 |
if ($color['red']>=256){ |
| 756 |
$color['red']=240; |
| 757 |
} |
| 758 |
if ($color['green']>=256){ |
| 759 |
$color['green']=240; |
| 760 |
} |
| 761 |
if ($color['blue']>=256){ |
| 762 |
$color['blue']=240; |
| 763 |
} |
| 764 |
$colors[]=substr('0'.dechex($color['red']),-2).substr('0'.dechex($color['green']),-2).substr('0'.dechex($color['blue']),-2); |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
if(isset($im)){ |
| 769 |
if($isImagIck===true){ |
| 770 |
$im->clear(); |
| 771 |
} |
| 772 |
else{ |
| 773 |
imagedestroy($im); |
| 774 |
} |
| 775 |
$im=null; |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
return $colors; |
| 780 |
} |