我正在使用此php代码将图像从pc上传到服务器
$ImageToLoad=mysql_real_escape_string($_POST['image_attached']);
if($ImageToLoad){
$token=$token;//variable
$ImageToLoad = str_replace('data:image/png;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace('data:image/jpeg;base64,', '', $ImageToLoad);
$ImageToLoad = str_replace(' ', '+', $ImageToLoad);
$fileData = base64_decode($ImageToLoad);
$destino_path="/images/$token/image.png";
file_put_contents($destino_path, $fileData);
}
它工作正常。
PROBLEM
我需要知道如何在服务器中存储图像之前调整它的大小/裁剪它们。否则它会保持相同的大小(当图像很大时会出现问题)
发布于 2018-06-10 00:50:06
如果我们想要从一个图像文件创建一个临时图像来调整大小,我们可以使用
imagecreatefromjpeg($filename);
或
imagecreatefrompng($filename);
如果我们想从blob创建一个临时图像,我们可以使用
imagecreatefromstring($blob);
所以,试一下这个:
<?php
$filename = 'folder_name/resized_image.png'; // output file name
$im = imagecreatefromstring($fileData);
$source_width = imagesx($im);
$source_height = imagesy($im);
$ratio = $source_height / $source_width;
$new_width = 300; // assign new width to new resized image
$new_height = $ratio * 300;
$thumb = imagecreatetruecolor($new_width, $new_height);
$transparency = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
imagefilledrectangle($thumb, 0, 0, $new_width, $new_height, $transparency);
imagecopyresampled($thumb, $im, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height);
imagepng($thumb, $filename, 9);
imagedestroy($im);
?>
https://stackoverflow.com/questions/50775658
复制相似问题