Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image filters #1322

Merged
merged 5 commits into from Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions inc/image/namespace.php
Expand Up @@ -111,7 +111,18 @@ function is_valid_image( $file, $filename, $is_stream = false ) {
$format = explode( '.', $filename );
$format = strtolower( end( $format ) ); // Extension
if ( ! ( 'jpg' === $format || 'jpeg' === $format || 'gif' === $format || 'png' === $format ) ) {
return false;

/**
* @since 5.5.0
*
* Filters if a image has a valid extension
*
* @param boolean $valid if is valid
* @param string $extension the extension of the file
*/
if ( ! apply_filters( 'pb_is_valid_image_extension', false, $format ) ) {
return false;
}
}

if ( $is_stream ) {
Expand All @@ -125,7 +136,16 @@ function is_valid_image( $file, $filename, $is_stream = false ) {
if ( IMAGETYPE_JPEG === $type || IMAGETYPE_GIF === $type || IMAGETYPE_PNG === $type ) {
return true;
} else {
return false;
/**
* @since 5.5.0
*
* Filters if a image is a valid image type
*
* @param boolean $valid if is valid
* @param string $type the type of the image
* @param file $file the file
*/
return apply_filters( 'pb_is_valid_image_type', false, $type, $file );
}
}

Expand Down
62 changes: 44 additions & 18 deletions inc/modules/export/epub/class-epub201.php
Expand Up @@ -2004,19 +2004,35 @@ protected function fetchAndSaveUniqueImage( $url, $fullpath ) {
// Basename without query string
$filename = explode( '?', basename( $url ) );

// isolate latex image service from WP, add file extension
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ( str_ends_with( $host, 'wordpress.com' ) || str_ends_with( $host, 'wp.com' ) ) && 'latex.php' === $filename[0] ) {
$filename = md5( array_pop( $filename ) );
// content-type = 'image/png'
$type = explode( '/', $response['headers']['content-type'] );
$type = array_pop( $type );
$filename = $filename . '.' . $type;
/**
* @since 5.5.0
*
* Filters the filename of a unique image
*
* @param string $filename the filename
* @param array $ori_filename the original filename
* @param object $response the response
* @param string $url the url
*/
$unique_filename = apply_filters( 'pb_epub201_fetchandsaveuniqueimage_filename', '', $filename, $response, $url );

if ( '' !== $unique_filename ) {
$filename = $unique_filename;
} else {
$filename = array_shift( $filename );
$filename = explode( '#', $filename )[0]; // Remove trailing anchors
$filename = sanitize_file_name( urldecode( $filename ) );
$filename = Sanitize\force_ascii( $filename );
// isolate latex image service from WP, add file extension
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ( str_ends_with( $host, 'wordpress.com' ) || str_ends_with( $host, 'wp.com' ) ) && 'latex.php' === $filename[0] ) {
$filename = md5( array_pop( $filename ) );
// content-type = 'image/png'
$type = explode( '/', $response['headers']['content-type'] );
$type = array_pop( $type );
$filename = $filename . '.' . $type;
} else {
$filename = array_shift( $filename );
$filename = explode( '#', $filename )[0]; // Remove trailing anchors
$filename = sanitize_file_name( urldecode( $filename ) );
$filename = Sanitize\force_ascii( $filename );
}
}

// A book with a lot of images can trigger "Fatal Error Too many open files" because tmpfiles are not closed until PHP exits
Expand All @@ -2034,12 +2050,22 @@ protected function fetchAndSaveUniqueImage( $url, $fullpath ) {
}

if ( $this->compressImages ) {
$format = explode( '.', $filename );
$format = strtolower( end( $format ) ); // Extension
try {
\Pressbooks\Image\resize_down( $format, $tmp_file );
} catch ( \Exception $e ) {
return '';
/**
Copy link
Contributor

@dac514 dac514 Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this filter into themeOptionsOverrides?

Set $this->compressImages = false next to this code:

https://github.com/pressbooks/pressbooks/blob/dev/inc/modules/export/epub/class-epub201.php#L403

So that it will be easier to spot what's going on if ever there's an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I see your point, but then it isn't the same filter anymore. How I implemented the filter, you can turn of compression on a per image base and that is needed here, as I don't want to turn of compression for all the images.

* @since 5.5.0
*
* Filters if a image should be compressed
*
* @param boolean $compress should it be compressed
* @param file $tmp_file the temp file
*/
if ( apply_filters( 'pb_epub201_fetchandsaveuniqueimage_compress', true, $tmp_file ) ) {
$format = explode( '.', $filename );
$format = strtolower( end( $format ) ); // Extension
try {
\Pressbooks\Image\resize_down( $format, $tmp_file );
} catch ( \Exception $e ) {
return '';
}
}
}

Expand Down