首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用php调整base64图像的大小或裁剪

使用php调整base64图像的大小或裁剪
EN

Stack Overflow用户
提问于 2018-06-09 23:09:00
回答 1查看 8K关注 0票数 1

我正在使用此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

我需要知道如何在服务器中存储图像之前调整它的大小/裁剪它们。否则它会保持相同的大小(当图像很大时会出现问题)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 00:50:06

如果我们想要从一个图像文件创建一个临时图像来调整大小,我们可以使用

imagecreatefromjpeg($filename);

imagecreatefrompng($filename);

如果我们想从blob创建一个临时图像,我们可以使用

imagecreatefromstring($blob);

imagecreatefromstring()

所以,试一下这个:

<?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);
?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50775658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档