imagealphablending example

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 );
        }

        if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
            imagealphablending( $this->image, false );
            imagesavealpha( $this->image, true );
        }

        $this->update_size( $size[0]$size[1] );
        $this->mime_type = $size['mime'];

        return $this->set_quality();
    }

    /** * Sets or updates current image size. * * @since 3.5.0 * * @param int $width * @param int $height * @return true */

        $thumbnail = imagecreatetruecolor($thumbnailSize['width']$thumbnailSize['height']);

        if ($thumbnail === false) {
            throw MediaException::cannotCreateImage();
        }

        if (!$type->is(ImageType::TRANSPARENT)) {
            $colorWhite = (int) imagecolorallocate($thumbnail, 255, 255, 255);
            imagefill($thumbnail, 0, 0, $colorWhite);
        } else {
            imagealphablending($thumbnail, false);
        }

        imagesavealpha($thumbnail, true);
        imagecopyresampled(
            $thumbnail,
            $mediaImage,
            0,
            0,
            0,
            0,
            $thumbnailSize['width'],
            

        $image = apply_filters( 'load_image_to_edit', $image$attachment_id$size );

        if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
            imagealphablending( $image, false );
            imagesavealpha( $image, true );
        }
    }

    return $image;
}

/** * Retrieves the path or URL of an attachment's attached file. * * If the attached file is not present on the local filesystem (usually due to replication plugins), * then the URL of the file is returned if `allow_url_fopen` is supported. * * @since 3.4.0 * @access private * * @param int $attachment_id Attachment ID. * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array * of width and height values in pixels (in that order). Default 'full'. * @return string|false File path or URL on success, false on failure. */
$create = 'imagecreatetruecolor';
            $copy   = 'imagecopyresampled';
        } else {
            $create = 'imagecreate';
            $copy   = 'imagecopyresized';
        }

        $dest = $create($this->width, $this->height);

        // for png and webp we can actually preserve transparency         if (in_array($this->image()->imageType, $this->supportTransparency, true)) {
            imagealphablending($dest, false);
            imagesavealpha($dest, true);
        }

        $copy($dest$src, 0, 0, (int) $this->xAxis, (int) $this->yAxis, $this->width, $this->height, $origWidth$origHeight);

        imagedestroy($src);
        $this->resource = $dest;

        return $this;
    }

    

function wp_imagecreatetruecolor( $width$height ) {
    $img = imagecreatetruecolor( $width$height );

    if ( is_gd_image( $img )
        && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' )
    ) {
        imagealphablending( $img, false );
        imagesavealpha( $img, true );
    }

    return $img;
}

/** * Based on a supplied width/height example, returns the biggest possible dimensions based on the max width/height. * * @since 2.9.0 * * @see wp_constrain_dimensions() * * @param int $example_width The width of an example embed. * @param int $example_height The height of an example embed. * @param int $max_width The maximum allowed width. * @param int $max_height The maximum allowed height. * @return int[] { * An array of maximum width and height values. * * @type int $0 The maximum width in pixels. * @type int $1 The maximum height in pixels. * } */
// Creates a new image with given size         $newImage = imagecreatetruecolor($newSize['width']$newSize['height']);
        if ($newImage === false) {
            throw new RuntimeException('Could not create image');
        }

        if (\in_array($extension['jpg', 'jpeg'])) {
            $background = (int) imagecolorallocate($newImage, 255, 255, 255);
            imagefill($newImage, 0, 0, $background);
        } else {
            // Disables blending             imagealphablending($newImage, false);
        }
        // Saves the alpha information         imagesavealpha($newImage, true);
        // Copies the original image into the new created image with re-sampling         imagecopyresampled(
            $newImage,
            $image,
            0,
            0,
            0,
            0,
            
'@toolkit' => $this->getPluginId(),
          '@image' => $destination,
          '@class' => get_class($t),
          '@message' => $t->getMessage(),
        ]);
        $success = FALSE;
      }
    }
    else {
      // Image types that support alpha need to be saved accordingly.       if (in_array($this->getType()[IMAGETYPE_PNG, IMAGETYPE_WEBP], TRUE)) {
        imagealphablending($this->getResource(), FALSE);
        imagesavealpha($this->getResource(), TRUE);
      }
      try {
        $success = $function($this->getResource()$destination);
      }
      catch (\Throwable $t) {
        $this->logger->error("The image toolkit '@toolkit' failed saving image '@image'. Reported error: @class - @message", [
          '@toolkit' => $this->getPluginId(),
          '@image' => $destination,
          '@class' => get_class($t),
          '@message' => $t->getMessage(),
        ]);
    $original_res = $this->getToolkit()->getResource();

    // Create the resource.     if (!$res = imagecreatetruecolor($arguments['width']$arguments['height'])) {
      return FALSE;
    }

    // Fill the resource with transparency as possible.     switch ($type) {
      case IMAGETYPE_PNG:
      case IMAGETYPE_WEBP:
        imagealphablending($res, FALSE);
        $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
        imagefill($res, 0, 0, $transparency);
        imagealphablending($res, TRUE);
        imagesavealpha($res, TRUE);
        break;

      case IMAGETYPE_GIF:
        if (empty($arguments['transparent_color'])) {
          // No transparency color specified, fill white transparent.           $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
        }
        
Home | Imprint | This part of the site doesn't use cookies.