How to change image size via PHP script

I am creating an application that requires you to convert a large image into a thumbnail via a PHP script, and then encode it to base64 so that I can send it via json to my Android application. I have a problem resizing the image. I need a php script to help me do this

+4
source share
6 answers

You can try this tutorial image resizing function

And also you can use this code for the resize function (GD).

<?php $thumb = new Imagick(); $thumb->readImage('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1); $thumb->writeImage('mythumb.gif'); $thumb->clear(); $thumb->destroy(); ?> Or, a shorter version of the same: <?php $thumb = new Imagick('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1); $thumb->writeImage('mythumb.gif'); $thumb->destroy(); ?> 

And also link to these links to resize the image.

1. YETANOTHERLinks

2. 9Lession

As well as Base64 conversion for image. Refer to the links

+3
source

The code below creates a function called createThumbs that takes three parameters. The first and second, respectively, are the path to the directory that contains the source images, and the path to the directory in which the thumbnails will be placed. The third parameter is the width you want for thumbnails.

 <?php function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) { // open the directory $dir = opendir( $pathToImages ); // loop through it, looking for any/all JPG files: while (false !== ($fname = readdir( $dir ))) { // parse path for the extension $info = pathinfo($pathToImages . $fname); // continue only if this is a JPEG image if ( strtolower($info['extension']) == 'jpg' ) { echo "Creating thumbnail for {$fname} <br />"; // load image and get image size $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a file imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" ); } } // close the directory closedir( $dir ); } // call createThumb function and pass to it as parameters the path // to the directory that contains images, the path to the directory // in which thumbnails will be placed and the thumbnail width. // We are assuming that the path will be a relative path working // both in the filesystem, and through the web for links createThumbs("upload/","upload/thumbs/",100); ?> 
+5
source

You can try imagick - http://php.net/manual/en/imagick.resizeimage.php to resize images. Sample code: To create a sketch:

 <?php $thumb = new Imagick(); $thumb->readImage('myimage.gif'); $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1); $thumb->writeImage('mythumb.gif'); $thumb->clear(); $thumb->destroy(); ?> 
+1
source

If you use GD, the code will be

 <?php // File and new size $filename = 'test.jpg'; // Content type header('Content-Type: image/jpeg'); // Get new sizes list($width, $height) = getimagesize($filename); $newwidth = YOUR REQUIRED WIDTH; $newheight = YOUR REQUIRED HEIGHT; // Load $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); // Resize imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Output imagejpeg($thumb); ?> 
+1
source

You can use, for example, TimThumb

 timthumb.php?src=img.jpg&h=height&w=width 

Then you should encode the image to base64: How to convert the image to base64 encoding?

+1
source

The message is a bit outdated, but if someone wants to, I just made this function

 function save_image_from_64($path, $name, $dimension, $original) { header("Content-Type: image/jpeg"); list($width,$height) = explode('x',$dimension); //Getting new height and width $img = str_replace('data:image/jpeg;base64,', '', $original); //Getting the base64 image $image = imagecreatefromstring(base64_decode($img)); $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image)); imagejpeg($new_image, $path.$name.'.jpg'); // saving the image } save_image_from_64("cdn/","test","800x200","data:image/jpeg;base64,/9j/4AAQS..."); 
0
source

Source: https://habr.com/ru/post/1492868/


All Articles