简体   繁体   中英

Set width and height of uploaded image

Can anyone pleas tell me how to resize the upload to image to a specific width and height (800*600). Then create the thumbnail of (400*300) and save thumbnails to a folder

NOTE: the file path must exist Using Imagemagick

// File from upload form
$tempFile = $_FILES['file']['tmp_name'];
// path to save image ex. $_SERVER['DOCUMENT_ROOT'].'/images/upload/';
$targetPath = PATH_TO_FINAL_IMAGE_FOLDER;
// Grab image info
$fileParts = pathinfo($_FILES['file']['name']);
// Any file name will work keeping extension or just use $_FILES['file']['name']
$newName = MY_FILE_NAME.".".$fileParts['extension'];
// File path and file
$targetFile = $targetPath.$newName
move_uploaded_file($tempFile,$targetFile);
// Resize Imagemagick only
// New file (any valid extenstion and any name)
$resizedFile = $targetPath."NEW_NAME.png";
// Using Imagemagick to change image ONLY if bigger then desired size
exec('convert "'.$targetFile.'[0]" -flatten -monitor -colorspace RGB -resize "800x600>" "'.$resizedFile.'"');
// Thumb file
$resizedFile = $targetPath."/thumb/NEW_NAME.png";
// create thumb
exec('convert "'.$targetFile.'[0]" -flatten -monitor -colorspace RGB -resize "400x300>" "'.$thumb.'"');

// Resize using GD

Using GD you can use:

$src = imagecreatefromjpeg("PATH_TO_FILE");
$dst = imagecreatetruecolor("NEW_WIDTH", "NEW_HEIGHT");
imagecopyresampled($dst, $src, 0, 0, 0, 0, "NEW_WIDTH", "NEW_HEIGHT", "START_WIDTH", "START_HEIGHT");

imagecreatefromjpeg

imagecreatetruecolor

imagecopyresampled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM