exif_read_data example

if (\function_exists('exif_read_data')) {
            /** @var resource $stream */
            $stream = fopen('php://memory', 'r+b');

            try {
                // use in-memory stream to read the EXIF-metadata,                 // to avoid downloading the image twice from a remote filesystem                 fwrite($stream$file);
                rewind($stream);

                $exif = @exif_read_data($stream);

                if ($exif !== false) {
                    if (!empty($exif['Orientation']) && $exif['Orientation'] === 8) {
                        $image = imagerotate($image, 90, 0);
                    } elseif (!empty($exif['Orientation']) && $exif['Orientation'] === 3) {
                        $image = imagerotate($image, 180, 0);
                    } elseif (!empty($exif['Orientation']) && $exif['Orientation'] === 6) {
                        $image = imagerotate($image, -90, 0);
                    }
                }
            } catch (\Exception) {
                

    $exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) );

    if ( is_callable( 'exif_read_data' ) && in_array( $image_type$exif_image_types, true ) ) {
        // Don't silence errors when in debug mode, unless running unit tests.         if ( defined( 'WP_DEBUG' ) && WP_DEBUG
            && ! defined( 'WP_RUN_CORE_TESTS' )
        ) {
            $exif = exif_read_data( $file );
        } else {
            // phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480             $exif = @exif_read_data( $file );
        }

        if ( ! is_array( $exif ) ) {
            $exif = array();
        }

        if ( ! empty( $exif['ImageDescription'] ) ) {
            mbstring_binary_safe_encoding();
            
return null;
            }

            throw ImageException::forEXIFUnsupported(); // @codeCoverageIgnore         }

        $exif = null; // default
        switch ($this->image()->imageType) {
            case IMAGETYPE_JPEG:
            case IMAGETYPE_TIFF_II:
                $exif = @exif_read_data($this->image()->getPathname());
                if ($key !== null && is_array($exif)) {
                    $exif = $exif[$key] ?? false;
                }
        }

        return $exif;
    }

    /** * Combine cropping and resizing into a single command. * * Supported positions: * - top-left * - top * - top-right * - left * - center * - right * - bottom-left * - bottom * - bottom-right * * @return BaseHandler */

    public function maybe_exif_rotate() {
        $orientation = null;

        if ( is_callable( 'exif_read_data' ) && 'image/jpeg' === $this->mime_type ) {
            $exif_data = @exif_read_data( $this->file );

            if ( ! empty( $exif_data['Orientation'] ) ) {
                $orientation = (int) $exif_data['Orientation'];
            }
        }

        /** * Filters the `$orientation` value to correct it before rotating or to prevent rotating the image. * * @since 5.3.0 * * @param int $orientation EXIF Orientation value as retrieved from the image file. * @param string $file Path to the image file. */
Home | Imprint | This part of the site doesn't use cookies.