简体   繁体   English

使用PHP调整大小并重命名PNG,JPG或GIF文件

[英]Resize and rename PNG, JPG or GIF file with PHP

is there any way, using PHP, that you can resize an image sent from a HTML form's WIDTH (Only PNG, JPG and GIF) to a max value of let's say 500px (so if the file is 350px wide there isn't any stretching), and rename it to a random 15 character name (eg "e19gy675jo5el7g.png") and save it to the image/ directory? 使用PHP,有什么方法可以将HTML表单的WIDTH(仅PNG,JPG和GIF)发送的图像的大小调整为最大值,例如500px(因此,如果文件的宽度为350px,则不会进行任何拉伸) ),并将其重命名为随机的15个字符的名称(例如“ e19gy675jo5el7g.png”)并将其保存到image /目录?

I have some code already but it doesn't resize the file and allows all file types to be uploaded (it only renames the file to a random name). 我已经有一些代码,但是它不调整文件大小,并允许所有文件类型上载(它仅将文件重命名为随机名称)。 I don't want to use the accept="image/*" HTML code in the form so if you could help me find a PHP solution that would be great. 我不想在表单中使用accept =“ image / *” HTML代码,因此如果您可以帮助我找到一个很棒的PHP解决方案。

Here's my PHP code... 这是我的PHP代码...

    <?php
 function findexts ($filename) 
 { 
 $filename = strtolower($filename) ; 
 $exts = split("[/\\.]", $filename) ; 
 $n = count($exts)-1; 
 $exts = $exts[$n]; 
 return $exts; 
 }     
 $ext = findexts ($_FILES['uploaded']['name']) ;   
 $ran = rand () ;   
 $ran2 = $ran.".";   
 $target = "image/";
 $target = $target . $ran2.$ext;   
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
 echo "The file has been uploaded as ".$ran2.$ext;
 } 
 else
 {
 echo "Sorry, there was a problem uploading your file.";
 }
 ?> 

And here's my HTML 这是我的HTML

<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<div>
<input name="uploaded" type="file" />
</div>
<br>
<button type="submit">Upload</button>
</form>
</body>
</html>

Sorry for the complicated question, I'm just quite new with PHP :-) 抱歉,这个复杂的问题对PHP来说是很新的:-)

Thanks in advance :-) 提前致谢 :-)

I don't know the whole code but you can do it by GD library of PHP.Use 我不知道全部代码,但是可以通过PHP的GD库来完成。

getimagesize($filename)

TO get the image details and check the width using it.If width is less than 500 then do not resize. 要获取图像细节并使用它检查宽度,如果宽度小于500,则不要调整大小。

Link that may help you: http://forums.phpfreaks.com/topic/210603-uploaded-image-change-width-and-height/ 可能会帮助您的链接: http : //forums.phpfreaks.com/topic/210603-uploaded-image-change-width-and-height/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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