Pergunta

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

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top