使用PHP调整图像大小并转换图像

使用PHP调整图像大小并转换图像,php,html,image,file,image-resizing,Php,Html,Image,File,Image Resizing,我的以下代码有一些问题。我的任务是开发一个PHP程序,可以上传图像,然后将其转换为jpg,并将其调整为最大宽度300px或高度300px。纵横比应与原始纵横比相同 最奇怪的是,函数“convertImage”输出如下内容: ����JFIF�����'�1�y�^�>�9<���H��^_������|6��a����B�����|%��^#��r�R:,������\z��c����='}U���S�s��$�bO�_��O$"���N74�����tл��ao/�8ԑ�G'�04

我的以下代码有一些问题。我的任务是开发一个PHP程序,可以上传图像,然后将其转换为jpg,并将其调整为最大宽度300px或高度300px。纵横比应与原始纵横比相同

最奇怪的是,函数“convertImage”输出如下内容:

����JFIF�����'�1�y�^�>�9<���H��^_������|6��a����B�����|%��^#��r�R:,������\z��c����='}U���S�s��$�bO�_��O$"���N74�����tл��ao/�8ԑ�G'�04���'��O�C��7��c1�99#8�׾}y�|�y�������3]ּg��G�t����Q��1x_����v��|8�n��^x�:mγ��[��iQ\>��]*���ĺ��-t{[��d��<-~x[���$���c������q�qӌ���d��=B9�3�<�y�;�I�תx��w�o�����~!|'��������T�7��U����~����ׇ͍5�J��M����,�kcas9�L���Ek[�f��3��랞�=pN2I�`�i���k�i�M��uBc�#���n���@rrFA�>�t�2y�|��c����׾G=r2x��xoW�M�i�5�O:[�yq$�vzu����Q-����Ok��[�Vk��V[���b�.n ��:�g T�*�*IB�)�rv�a��'�)6��vc�e9F��)4����z$��0��?��r8 ��1����3߸9�k�?�}/��oi�Ե�x�h��9��eS��!�����-tD�P��jw�}
如果你需要任何其他信息,请随时问我


谢谢大家

有关PHP中的图像大小调整,请参阅下面的链接

https://code.tutsplus.com/tutorials/image-resizing-made-easy-with-php--net-10362

我在卡达的帮助下解决了我的问题。谢谢

我不得不把密码从

imagedestroy($imageTemp);
imagejpeg($resizedImg, $output);
imagedestroy($resizedImg);


您的新图像计算比率公式错误。这是固定的:

if ($ratio > 1 ) {
    $width  = 300 * $ratio;
    $heigth = 300;
} else {
    $width = 300;
    $heigth = 300 / $ratio;
}  

imagejpeg
中的
$output
不是您想象的那样。它应该是一个带有文件路径或开放资源流量的字符串。如果该值为null,则图像流量将直接发送到浏览器。因为你的参数是一个非集合变量,它被理解为null,你得到的是最后一种情况,这就是为什么你有这个意外的输出,实际上你们都错了。您正在计算最小宽度为300的图像大小@Schlodi正在计算正确的尺寸,但他/她正在交换尺寸。对于
$ratio>1
,公式应该是
$width=300/$ratio
$height=300*$ratio
对于
$ratio 1
具有“单位”
width²/height
,而不是
width
https://code.tutsplus.com/tutorials/image-resizing-made-easy-with-php--net-10362
imagedestroy($imageTemp);
imagejpeg($resizedImg, $output);
imagedestroy($resizedImg);
imagedestroy($imageTemp);

//starting an output buffer to get the data
ob_start();

imagejpeg($resizedImg);

//here we get the data
$output = ob_get_clean();

imagedestroy($resizedImg);
if ($ratio > 1 ) {
    $width  = 300 * $ratio;
    $heigth = 300;
} else {
    $width = 300;
    $heigth = 300 / $ratio;
}