Skip to content

Commit

Permalink
Merge pull request #730 from kprajapatii/master
Browse files Browse the repository at this point in the history
Add support to resize webp image for avatar/banner - FIXED
  • Loading branch information
kprajapatii authored Dec 18, 2023
2 parents a001e9a + b009be5 commit 8836386
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
74 changes: 49 additions & 25 deletions includes/helpers/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,56 +132,80 @@ function uwp_string_to_options($input = '', $translated = false)
*
* @return mixed Resized image.
*/
function uwp_resizeThumbnailImage($thumb_image_name, $image, $x, $y, $src_w, $src_h, $scale){
function uwp_resizeThumbnailImage( $thumb_image_name, $image, $x, $y, $src_w, $src_h, $scale, $wp_error = false ) {
uwp_set_php_limits();
// ignore image creation warnings
@ini_set('gd.jpeg_ignore_warning', 1);

// Ignore image creation warnings
@ini_set( 'gd.jpeg_ignore_warning', 1 );

$newImageWidth = ceil( $src_w * $scale );
$newImageHeight = ceil( $src_h * $scale );

/** @noinspection PhpUnusedLocalVariableInspection */
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
list( $imagewidth, $imageheight, $imageType ) = getimagesize( $image );
$imageType = image_type_to_mime_type( $imageType );

if ( function_exists( 'wp_crop_image' ) ) {
$wp_crop = ! in_array( $imageType, array( 'image/gif', 'image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/x-png' ) ) ? true : false;
$wp_crop = apply_filters( 'uwp_resize_image_use_wp_crop_image', $wp_crop, $imageType, $image, $thumb_image_name, $x, $y, $src_w, $src_h, $scale );

if ( $wp_crop ) {
$cropped = wp_crop_image( $image, $x, $y, $src_w, $src_h, $newImageWidth, $newImageHeight, false, $thumb_image_name );

$newImageWidth = ceil($src_w * $scale);
$newImageHeight = ceil($src_h * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
if ( $wp_error && is_wp_error( $cropped ) ) {
return $cropped;
}

return $thumb_image_name;
}
}

$newImage = imagecreatetruecolor( $newImageWidth, $newImageHeight );
$source = false;
switch($imageType) {

switch( $imageType ) {
case "image/gif":
$source=imagecreatefromgif($image);
$source = imagecreatefromgif( $image );
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source=imagecreatefromjpeg($image);
$source = imagecreatefromjpeg( $image );
break;
case "image/png":
case "image/x-png":
$source=imagecreatefrompng($image);
if(apply_filters('uwp_keep_png_transperent', true, $thumb_image_name, $image, $x, $y, $src_w, $src_h)){
$background = imagecolorallocate($newImage , 0, 0, 0);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
}
$source = imagecreatefrompng( $image );
if ( apply_filters( 'uwp_keep_png_transperent', true, $thumb_image_name, $image, $x, $y, $src_w, $src_h ) ) {
$background = imagecolorallocate( $newImage, 0, 0, 0 );

imagecolortransparent( $newImage, $background );
imagealphablending( $newImage, false );
imagesavealpha( $newImage, true );
}
break;
}
imagecopyresampled($newImage,$source,0,0,$x,$y,$newImageWidth, $newImageHeight, $src_w, $src_h);
$quality = apply_filters( 'uwp_resize_thumb_quality', 100);
switch($imageType) {

imagecopyresampled( $newImage, $source, 0, 0, $x, $y, $newImageWidth, $newImageHeight, $src_w, $src_h );

$quality = apply_filters( 'uwp_resize_thumb_quality', 100 );

switch( $imageType ) {
case "image/gif":
imagegif($newImage, $thumb_image_name);
imagegif( $newImage, $thumb_image_name );
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage, $thumb_image_name, $quality);
imagejpeg( $newImage, $thumb_image_name, $quality );
break;
case "image/png":
case "image/x-png":
imagepng($newImage, $thumb_image_name);
imagepng( $newImage, $thumb_image_name );
break;
}

chmod($thumb_image_name, 0777);
chmod( $thumb_image_name, 0777 );

return $thumb_image_name;
}

Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ Yes, you can customize it with Elementor, but also with Gutenberg, Divi, Beaver

== Changelog ==

= 1.2.4 =
* Add support to resize webp image for avatar/banner - FIXED

= 1.2.3.24 =
* Tested up to WP version 6.4 - CHANGED

Expand Down

0 comments on commit 8836386

Please sign in to comment.