imagecreatefromstring example

->setParameter('{{ width }}', $width)
                ->setParameter('{{ height }}', $height)
                ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
                ->addViolation();
        }

        if ($constraint->detectCorrupted) {
            if (!\function_exists('imagecreatefromstring')) {
                throw new LogicException('Corrupted images detection requires installed and enabled GD extension.');
            }

            $resource = @imagecreatefromstring(file_get_contents($value));

            if (false === $resource) {
                $this->context->buildViolation($constraint->corruptedMessage)
                    ->setCode(Image::CORRUPTED_IMAGE_ERROR)
                    ->addViolation();

                return;
            }

            imagedestroy($resource);
        }
    }
/** * Determines the extension of the file according to * the given path and calls the right creation * method for the image extension * * @throws RuntimeException * * @return resource */
    private function createImageResource(string $fileContent, string $imagePath)
    {
        $image = imagecreatefromstring($fileContent);
        if ($image === false) {
            throw new RuntimeException(sprintf('Image is not in a recognized format (%s)', $imagePath));
        }

        return $image;
    }

    /** * Returns the extension of the file with passed path */
    private function getImageExtension(string $path): string
    {
->setParameter('{{ width }}', $width)
                ->setParameter('{{ height }}', $height)
                ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
                ->addViolation();
        }

        if ($constraint->detectCorrupted) {
            if (!\function_exists('imagecreatefromstring')) {
                throw new LogicException('Corrupted images detection requires installed and enabled GD extension.');
            }

            $resource = @imagecreatefromstring(file_get_contents($value));

            if (false === $resource) {
                $this->context->buildViolation($constraint->corruptedMessage)
                    ->setCode(Image::CORRUPTED_IMAGE_ERROR)
                    ->addViolation();

                return;
            }

            imagedestroy($resource);
        }
    }
if ( ! $file_contents ) {
            return new WP_Error( 'error_loading_image', __( 'File does not exist?' )$this->file );
        }

        // WebP may not work with imagecreatefromstring().         if (
            function_exists( 'imagecreatefromwebp' ) &&
            ( 'image/webp' === wp_get_image_mime( $this->file ) )
        ) {
            $this->image = @imagecreatefromwebp( $this->file );
        } else {
            $this->image = @imagecreatefromstring( $file_contents );
        }

        if ( ! is_gd_image( $this->image ) ) {
            return new WP_Error( 'invalid_image', __( 'File is not an image.' )$this->file );
        }

        $size = wp_getimagesize( $this->file );

        if ( ! $size ) {
            return new WP_Error( 'invalid_image', __( 'Could not read image size.' )$this->file );
        }

        
$this->process($cmd);
    }

    /** * Return the width of an image. * * @return int */
    public function _getWidth()
    {
        return imagesx(imagecreatefromstring(file_get_contents($this->resource)));
    }

    /** * Return the height of an image. * * @return int */
    public function _getHeight()
    {
        return imagesy(imagecreatefromstring(file_get_contents($this->resource)));
    }

    
if ( ! is_file( $file ) ) {
        /* translators: %s: File name. */
        return sprintf( __( 'File “%s” does not exist?' )$file );
    }

    if ( ! function_exists('imagecreatefromstring') )
        return __('The GD image library is not installed.');

    // Set artificially high because GD uses uncompressed images in memory.     wp_raise_memory_limit( 'image' );

    $image = imagecreatefromstring( file_get_contents( $file ) );

    if ( ! is_gd_image( $image ) ) {
        /* translators: %s: File name. */
        return sprintf( __( 'File “%s” is not an image.' )$file );
    }

    return $image;
}

/** * Scale down an image to fit a particular size and save a new copy of the image. * * The PNG transparency will be preserved using the function, as well as the * image type. If the file going in is PNG, then the resized image is going to * be PNG. The only supported image types are PNG, GIF, and JPEG. * * Some functionality requires API to exist, so some PHP version may lose out * support. This is not the fault of WordPress (where functionality is * downgraded, not actual defects), but of your PHP version. * * @since 2.5.0 * @deprecated 3.5.0 Use wp_get_image_editor() * @see wp_get_image_editor() * * @param string $file Image file path. * @param int $max_w Maximum width to resize to. * @param int $max_h Maximum height to resize to. * @param bool $crop Optional. Whether to crop image or resize. Default false. * @param string $suffix Optional. File suffix. Default null. * @param string $dest_path Optional. New image file path. Default null. * @param int $jpeg_quality Optional. Image quality percentage. Default 90. * @return mixed WP_Error on failure. String with new destination path. */
/** @var MediaFolderEntity $folder */
        $folder = $this->mediaFolderRepository->search($criteria$context)->get($mediaFolderId);
        $media->setMediaFolder($folder);
    }

    private function getImageResource(MediaEntity $media): \GdImage
    {
        $filePath = $this->urlGenerator->getRelativeMediaUrl($media);
        /** @var string $file */
        $file = $this->getFileSystem($media)->read($filePath);
        $image = @imagecreatefromstring($file);
        if ($image === false) {
            throw MediaException::thumbnailNotSupported($media->getId());
        }

        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
Home | Imprint | This part of the site doesn't use cookies.