How to zoom image using php

6.3k views Asked by At

There is any possibility to zoom an image in php? I've searched on google but nothing util found.

If someone has any tips please give it to me. Thank you

3

There are 3 answers

0
Rachel Gallen On

If you follow the imagecopyresampled php example (and make it large instead of smaller - (smaller is demonstrated in the php.net example), and if you are PASSING a js variable, then you would get something like the following php:

<?php
// The file
$imgin = $_POST["dogimg"];  
      //e.g if your img is called dogimg!
$percent = 1.4; //140%

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($imgin);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_out = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($imgin);
imagecopyresampled($image_out, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_out, null, 100);
?>

However, I should point out that you should be careful when working with file types such as images and I would advise that you put in some error handling to check that the file is an image at the start (preferably when you upload the image to begin with, if you are uploading the images via a website).

If you're working with pre-uploaded images that you have uploaded on the server, then you should be good to go.

0
LF-DevJourney On

use imagemagic

$geometry = '142x106';
$resize_geo = $geometry.'^';
$crop_geo = $geometry.'+0+0';
`convert $pic_original_path -resize $resize_geo -gravity Center -crop $crop_geo $pic_resized_path`;
0
devondre On

Imagecopyresampled solution here:

$zoom=120; //to zoom 120%
$width = imagesx($myimage);
$height = imagesy($myimage);

$new_width = $width * $zoom / 100;
$new_height = $height * $zoom / 100;

imagecopyresampled($image_p, $myimage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);