PHP 8.5.0 RC 2 available for testing

Imagick::scaleImage

(PECL imagick 2, PECL imagick 3)

Imagick::scaleImage β€” Scales the size of an image

Опис

public Imagick::scaleImage(
    int $columns,
    int $rows,
    bool $bestfit = false,
    bool $legacy = false
): bool

Scales the size of an image to the given dimensions. The other parameter will be calculated if 0 is passed as either param.

ЗауваТСння: ΠŸΠΎΠ²Π΅Π΄Ρ–Π½ΠΊΠ° ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° bestfit змінилася Π² Imagick 3.0.0. Π Π°Π½Ρ–ΡˆΠ΅ Π²Ρ–Π½ Π½Π΅ діяв Π½Π° зобраТСння Ρ€ΠΎΠ·ΠΌΡ–Ρ€Ρ–Π² 400x400 Ρ‚Π° 200x150. ΠŸΠΎΡ‡ΠΈΠ½Π°ΡŽΡ‡ΠΈ Π· Imagick 3.0.0, Ρ‚Π°ΠΊΡ– зобраТСння ΠΎΡ‚Ρ€ΠΈΠΌΠ°ΡŽΡ‚ΡŒ Ρ€ΠΎΠ·ΠΌΡ–Ρ€ 400x300, ΠΎΡΠΊΡ–Π»ΡŒΠΊΠΈ Ρ†Π΅ "Π½Π°ΠΉΠΊΡ€Π°Ρ‰Π΅ ΠΏΡ–Π΄Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ" для Π·Π°Π΄Π°Π½ΠΈΡ… Ρ€ΠΎΠ·ΠΌΡ–Ρ€Ρ–Π². Π―ΠΊΡ‰ΠΎ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ bestfit Π²ΠΈΠΊΠΎΡ€ΠΈΡΡ‚ΠΎΠ²ΡƒΡ”Ρ‚ΡŒΡΡ, Π½Π΅ΠΎΠ±Ρ…Ρ–Π΄Π½ΠΎ Π²ΠΊΠ°Π·Π°Ρ‚ΠΈ як висоту, Ρ‚Π°ΠΊ Ρ– ΡˆΠΈΡ€ΠΈΠ½Ρƒ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΈ

columns

rows

bestfit

ЗначСння, Ρ‰ΠΎ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°ΡŽΡ‚ΡŒΡΡ

ΠŸΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” true Π² Ρ€Π°Π·Ρ– успіху.

Помилки/Π²ΠΈΠΊΠ»ΡŽΡ‡Π΅Π½Π½Ρ

ΠšΠΈΠ΄Π°Ρ” ImagickException Π² Ρ€Π°Π·Ρ– ΠΏΠΎΠΌΠΈΠ»ΠΊΠΈ.

Π–ΡƒΡ€Π½Π°Π» Π·ΠΌΡ–Π½

ВСрсія Опис
PECL imagick 2.1.0 Added optional fit parameter. This method now supports proportional scaling. Pass zero as either parameter for proportional scaling.

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ΠΈ

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #1 Imagick::scaleImage()

<?php
function scaleImage($imagePath) {
$imagick = new \Imagick(realpath($imagePath));
$imagick->scaleImage(150, 150, true);
header("Content-Type: image/jpg");
echo
$imagick->getImageBlob();
}

?>

οΌ‹add a note

User Contributed Notes 5 notes

up
28
benford at bluhelix dot com ΒΆ
16 years ago
If anyone finds "The other parameter will be calculated if 0 is passed as either param. " to be a bit confusing, it means approximately this:

<?php
$im
= new Imagick('example.jpg');
$im->scaleImage(300, 0);
?>

This scales the image such that it is now 300 pixels wide, and automatically calculates the height to keep the image at the same aspect ratio.

<?php
$im
= new Imagick('example.jpg');
$im->scaleImage(0, 300);
?>

Similarly, this example scales the image to make it 300 pixels tall, and the method automatically recalculates the image's height to maintain the aspect ratio.
up
9
vincent dot hoen at gmail dot com ΒΆ
18 years ago
Here is an easy way to resize an animated gif :

$picture = new Imagick('animated_gif.gif');

foreach($picture as $frame){
$frame->scaleImage($width, $height);
}
up
6
octave at web dot de ΒΆ
16 years ago
When using the "fit = true" option, the image will only scale down, but never scale up:

<?php
$im
= new Imagick('1600x1200.jpg');

$im->scaleImage(2000, 1500, true); // => 1600x1200

$im->scaleImage(1000, 500, true); // => 666x500
?>
up
5
clickconvert at gmail dot com ΒΆ
12 years ago
Need to resize portrait and landscape images (and convert to 72ppi)? These will fit an area of 800x600 without distorting, no matter how tall or wide.

<?php
$img
= new Imagick($img_loc.$file);
$img->setImageResolution(72,72);
$img->resampleImage(72,72,imagick::FILTER_UNDEFINED,1);
$img->scaleImage(800,0);
$d = $img->getImageGeometry();
$h = $d['height'];
if(
$h > 600) {
$img->scaleImage(0,600);
$img->writeImage($resized_loc.$file);
} else {
$img->writeImage($resized_loc.$file);
}
$img->destroy();
?>
up
2
agamemnus at flyingsoft dot pw ΒΆ
11 years ago
Warning: this will blur your edges in possibly unexpected ways. For better control, use resizeImage, instead.
To Top