imagecopyresampled example

$w   = imagesx( $img );
    $h   = imagesy( $img );
    $dst = wp_imagecreatetruecolor( $w$h );

    if ( is_gd_image( $dst ) ) {
        $sx = $vert ? ( $w - 1 ) : 0;
        $sy = $horz ? ( $h - 1 ) : 0;
        $sw = $vert ? -$w : $w;
        $sh = $horz ? -$h : $h;

        if ( imagecopyresampled( $dst$img, 0, 0, $sx$sy$w$h$sw$sh ) ) {
            imagedestroy( $img );
            $img = $dst;
        }
    }

    return $img;
}

/** * Crops an image resource. Internal use only. * * @since 2.9.0 * * @ignore * @param resource|GdImage $img Image resource or GdImage instance. * @param float $x Source point x-coordinate. * @param float $y Source point y-coordinate. * @param float $w Source width. * @param float $h Source height. * @return resource|GdImage (maybe) cropped image resource or GdImage instance. */
$height = $this->getToolkit()->getHeight();
    $original_resource = $this->getToolkit()->getResource();
    $original_type = $this->getToolkit()->getType();
    $data = [
      'width' => $width,
      'height' => $height,
      'extension' => $arguments['extension'],
      'transparent_color' => $this->getToolkit()->getTransparentColor(),
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource()$original_resource, 0, 0, 0, 0, $width$height$width$height)) {
        imagedestroy($original_resource);
        return TRUE;
      }
      // In case of error, reset resource and type to as it was.       $this->getToolkit()->setResource($original_resource);
      $this->getToolkit()->setType($original_type);
    }
    return FALSE;
  }

}
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,
            $newSize['width'],
            $newSize['height'],
            $originalSize['width'],
            $originalSize['height']
        );

        
    // the original resource on it with resampling. Destroy the original     // resource upon success.     $original_resource = $this->getToolkit()->getResource();
    $data = [
      'width' => $arguments['width'],
      'height' => $arguments['height'],
      'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
      'transparent_color' => $this->getToolkit()->getTransparentColor(),
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource()$original_resource, 0, 0, $arguments['x']$arguments['y']$arguments['width']$arguments['height']$arguments['width']$arguments['height'])) {
        imagedestroy($original_resource);
        return TRUE;
      }
      else {
        // In case of failure, destroy the temporary resource and restore         // the original one.         imagedestroy($this->getToolkit()->getResource());
        $this->getToolkit()->setResource($original_resource);
      }
    }
    return FALSE;
  }

    protected function _resize( $max_w$max_h$crop = false ) {
        $dims = image_resize_dimensions( $this->size['width']$this->size['height']$max_w$max_h$crop );

        if ( ! $dims ) {
            return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' )$this->file );
        }

        list( $dst_x$dst_y$src_x$src_y$dst_w$dst_h$src_w$src_h ) = $dims;

        $resized = wp_imagecreatetruecolor( $dst_w$dst_h );
        imagecopyresampled( $resized$this->image, $dst_x$dst_y$src_x$src_y$dst_w$dst_h$src_w$src_h );

        if ( is_gd_image( $resized ) ) {
            $this->update_size( $dst_w$dst_h );
            return $resized;
        }

        return new WP_Error( 'image_resize_error', __( 'Image resize failed.' )$this->file );
    }

    /** * Create multiple smaller images from a single source. * * Attempts to create all sub-sizes and returns the meta data at the end. This * may result in the server running out of resources. When it fails there may be few * "orphaned" images left over as the meta data is never returned and saved. * * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates * the new images one at a time and allows for the meta data to be saved after * each new image is created. * * @since 3.5.0 * * @param array $sizes { * An array of image size data arrays. * * Either a height or width must be provided. * If one of the two is set to null, the resize will * maintain aspect ratio according to the source image. * * @type array ...$0 { * Array of height, width values, and whether to crop. * * @type int $width Image width. Optional if `$height` is specified. * @type int $height Image height. Optional if `$width` is specified. * @type bool|array $crop Optional. Whether to crop the image. Default false. * } * } * @return array An array of resized images' metadata by size. */
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'],
            $thumbnailSize['height'],
            $originalImageSize['width'],
            $originalImageSize['height']
        );

        
    // the original resource on it with resampling. Destroy the original     // resource upon success.     $original_resource = $this->getToolkit()->getResource();
    $data = [
      'width' => $arguments['width'],
      'height' => $arguments['height'],
      'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
      'transparent_color' => $this->getToolkit()->getTransparentColor(),
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource()$original_resource, 0, 0, 0, 0, $arguments['width']$arguments['height']imagesx($original_resource)imagesy($original_resource))) {
        imagedestroy($original_resource);
        return TRUE;
      }
      else {
        // In case of failure, destroy the temporary resource and restore         // the original one.         imagedestroy($this->getToolkit()->getResource());
        $this->getToolkit()->setResource($original_resource);
      }
    }
    return FALSE;
  }
Home | Imprint | This part of the site doesn't use cookies.