Analysis result
Scanned at : 2023-10-14
Analyzed by : Coding Standard PSR12
Your PHP Code Snippet
<?php
namespace WebAvifConverter\GeneratorUtils;
/**
* Convert image to WebP format
*
* @param string $path Path to the image file.
* @param int $quality Image quality (0 to 100, default is 80).
*
* @return string|void Returns the relative path to the converted WebP image or void if unable to create an image resource.
*/
function p($a, $die = 0){
echo '<pre>';
echo print_r($a);
echo '</pre>';
if ($die) { wp_die(); }
}
function generate_jpeg(string $path, int $quality = 80){
$output_path = dirname($path);
$extension = pathinfo($path, PATHINFO_EXTENSION);
$filename = basename($path);
$image = null;
if ($extension === 'jpeg' || $extension === 'jpg') {
$image = imagecreatefromjpeg($path);
} elseif ($extension === 'png') {
$image = imagecreatefrompng($path);
} elseif ($extension === 'gif') {
$image = imagecreatefromgif($path);
} else {
return; // Unsupported format.
}
if (!$image) {
return; // Unable to create image resource, possibly unsupported format.
}
imagejpeg($image, $output_path . '/' . $filename , $quality);
imagedestroy($image);
$directories = explode('/', dirname($path));
$upload_subdir = implode('/', array_slice($directories, -2));
return $upload_subdir . '/' . $filename;
}
function generate_webp(string $path, int $quality = 80)
{
$output_path = dirname($path);
$extension = pathinfo($path, PATHINFO_EXTENSION);
$filename = basename($path, '.' . $extension);
$image = null;
if ($extension === 'jpeg' || $extension === 'jpg') {
$image = imagecreatefromjpeg($path);
} elseif ($extension === 'png') {
$image = imagecreatefrompng($path);
} elseif ($extension === 'gif') {
$image = imagecreatefromgif($path);
}
if (!$image) {
return; // Unable to create image resource, possibly unsupported format.
}
imagewebp($image, $output_path . '/' . $filename . '.webp', $quality);
imagedestroy($image);
$directories = explode('/', dirname($path));
$upload_subdir = implode('/', array_slice($directories, -2));
return $upload_subdir . '/' . $filename . '.webp';
}
/**
* Convert image to AVIF format
*
* @param string $path Path to the image file.
* @param int $quality Image quality (0 to 100, default is 80).
*
* @return string|void Returns the relative path to the converted AVIF image or void if unable to create an image resource.
*/
function generate_avif(string $path, int $quality = 80)
{
$output_path = dirname($path);
$extension = pathinfo($path, PATHINFO_EXTENSION);
$filename = basename($path, '.' . $extension);
$image = null;
if ($extension === 'jpeg' || $extension === 'jpg') {
$image = imagecreatefromjpeg($path);
} elseif ($extension === 'png') {
$image = imagecreatefrompng($path);
} elseif ($extension === 'gif') {
$image = imagecreatefromgif($path);
} else {
return; // Unsupported format.
}
if (!$image) {
return; // Unable to create image resource, possibly unsupported format.
}
imageavif($image, $output_path . '/' . $filename . '.avif', $quality);
imagedestroy($image);
$directories = explode('/', dirname($path));
$upload_subdir = implode('/', array_slice($directories, -2));
return $upload_subdir . '/' . $filename . '.avif';
}
function generate_jpeg_sizes($attachment_id, $quality){
$metadata = wp_get_attachment_metadata($attachment_id);
if (empty($metadata)) { return; }
$original_image_path = wp_get_upload_dir()['basedir'] . '/' . $metadata['file'];
$image_subdir_path = wp_get_upload_dir()['basedir'] . '/' . dirname($metadata['file']);
$metadata['quality_jpeg'] = $quality;
// skip svg files path info
if (pathinfo($original_image_path, PATHINFO_EXTENSION) === 'svg') {
return;
}
// p($metadata['sizes'], 1);
foreach($metadata['sizes'] as $size_name => $size){
$subsize_path = $image_subdir_path . '/' . $size['file'];
$image = wp_get_image_editor($subsize_path);
$image->set_quality( $quality );
$saved = $image->save($subsize_path);
$metadata['sizes'][$size_name]['filesize'] = filesize($subsize_path);
}
wp_update_attachment_metadata($attachment_id, $metadata);
};
function generate_webp_sizes($attachment_id, $quality){
$metadata = wp_get_attachment_metadata($attachment_id);
if (empty($metadata)) { return; }
$image_subdir_path = wp_get_upload_dir()['basedir'] . '/' . dirname($metadata['file']);
$metadata['quality_webp'] = $quality;
$metadata['sizes_webp'] = [];
$metadata['file_webp'] = generate_webp(
wp_get_upload_dir()['basedir'] . '/' . $metadata['file'],
$quality
);
$metadata['webp_filesize'] = filesize(wp_get_upload_dir()['basedir'] . '/'. $metadata['file_webp']);
$original_image_path = wp_get_upload_dir()['basedir'] . '/' . $metadata['file'];
// skip svg files path info
if (pathinfo($original_image_path, PATHINFO_EXTENSION) === 'svg') {
return;
}
foreach($metadata['sizes'] as $size_name => $size){
$original_subsize_path = $image_subdir_path . '/' . $size['file'];
//create or overwrite the subsize image
$subsize_subpath = generate_webp($original_subsize_path, $quality);
$metadata['sizes_webp'][$size_name] = [
'file' => basename($subsize_subpath),
'width' => $size['width'],
'height' => $size['height'],
'mime-type' => 'image/webp',
'filesize' => filesize(wp_get_upload_dir()['basedir'] . '/' . $subsize_subpath)
];
}
wp_update_attachment_metadata($attachment_id, $metadata);
};
function generate_avif_sizes($attachment_id, $quality){
$metadata = wp_get_attachment_metadata($attachment_id);
if (empty($metadata)) { return; }
$image_subdir_path = wp_get_upload_dir()['basedir'] . '/' . dirname($metadata['file']);
$metadata['quality_avif'] = $quality;
$metadata['sizes_avif'] = [];
$metadata['file_avif'] = generate_avif(
wp_get_upload_dir()['basedir'] . '/' . $metadata['file'],
$quality
);
$metadata['avif_filesize'] = filesize(wp_get_upload_dir()['basedir'] . '/'. $metadata['file_avif']);
$original_image_path = wp_get_upload_dir()['basedir'] . '/' . $metadata['file'];
// skip svg files path info
if (pathinfo($original_image_path, PATHINFO_EXTENSION) === 'svg') {
return;
}
foreach($metadata['sizes'] as $size_name => $size){
$original_subsize_path = $image_subdir_path . '/' . $size['file'];
//create or overwrite the subsize image
$subsize_subpath = generate_avif($original_subsize_path, $quality);
$metadata['sizes_avif'][$size_name] = [
'file' => basename($subsize_subpath),
'width' => $size['width'],
'height' => $size['height'],
'mime-type' => 'image/avif',
'filesize' => filesize(wp_get_upload_dir()['basedir'] . '/' . $subsize_subpath)
];
}
wp_update_attachment_metadata($attachment_id, $metadata);
};
Thanks for your like.
Your like has been removed.
Your dislike has been saved.
Your dislike has been removed.
An error occurred: Unable to save your like or dislike.
Errors: 35
Warning: 3
Severity | Type | Message | Location |
---|---|---|---|
5 | WARNING | A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 14 and the first side effect is on line 141. | Line 1, column 1 |
5 | ERROR | End of line character is invalid; expected "\n" but found "\r\n" | Line 1, column 1 |
5 | WARNING | Line exceeds 120 characters; contains 123 characters | Line 11, column 12 |
5 | ERROR | Line indented incorrectly; expected 0 spaces, found 1 | Line 14, column 2 |
5 | ERROR | Opening brace should be on a new line | Line 14, column 26 |
5 | ERROR | Newline required after opening brace | Line 18, column 15 |
5 | ERROR | Closing brace must be on a line by itself | Line 18, column 27 |
5 | ERROR | Closing brace indented incorrectly; expected 1 spaces, found 0 | Line 19, column 1 |
5 | ERROR | Opening brace should be on a new line | Line 21, column 56 |
5 | ERROR | Space found before comma in argument list | Line 41, column 54 |
5 | WARNING | Line exceeds 120 characters; contains 123 characters | Line 82, column 12 |
5 | ERROR | Opening brace should be on a new line | Line 115, column 55 |
5 | ERROR | Newline required after opening brace | Line 118, column 27 |
5 | ERROR | Closing brace must be on a line by itself | Line 118, column 37 |
5 | ERROR | Whitespace found at end of line | Line 119, column 1 |
5 | ERROR | Expected 1 space(s) after FOREACH keyword; 0 found | Line 131, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 131, column 54 |
5 | ERROR | Space after opening parenthesis of function call prohibited | Line 134, column 17 |
5 | ERROR | Expected 0 spaces before closing parenthesis; 1 found | Line 134, column 39 |
5 | ERROR | Closing brace must not be followed by any comment or statement on the same line | Line 141, column 1 |
5 | ERROR | Opening brace should be on a new line | Line 144, column 55 |
5 | ERROR | Newline required after opening brace | Line 146, column 27 |
5 | ERROR | Closing brace must be on a line by itself | Line 146, column 37 |
5 | ERROR | Whitespace found at end of line | Line 147, column 1 |
5 | ERROR | Expected at least 1 space before "."; 0 found | Line 157, column 79 |
5 | ERROR | Expected 1 space(s) after FOREACH keyword; 0 found | Line 166, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 166, column 54 |
5 | ERROR | Whitespace found at end of line | Line 168, column 1 |
5 | ERROR | Closing brace must not be followed by any comment or statement on the same line | Line 182, column 1 |
5 | ERROR | Opening brace should be on a new line | Line 185, column 55 |
5 | ERROR | Newline required after opening brace | Line 187, column 27 |
5 | ERROR | Closing brace must be on a line by itself | Line 187, column 37 |
5 | ERROR | Whitespace found at end of line | Line 188, column 1 |
5 | ERROR | Expected at least 1 space before "."; 0 found | Line 198, column 79 |
5 | ERROR | Expected 1 space(s) after FOREACH keyword; 0 found | Line 207, column 5 |
5 | ERROR | Expected 1 space(s) after closing parenthesis; found 0 | Line 207, column 54 |
5 | ERROR | Closing brace must not be followed by any comment or statement on the same line | Line 223, column 1 |
5 | ERROR | Expected 1 newline at end of file; 0 found | Line 223, column 2 |