ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

更改图像大小 – PHP

2019-09-29 13:43:52  阅读:204  来源: 互联网

标签:php image-resizing image-processing


我最近创建了一个上传功能,但我不知道如何将宽度和高度更改为75px …我尝试了一个我通过Google找到的代码,但我得到了这个错误:

( ! ) Fatal error: Class 'Imagick' not found in C:\wamp\www\Legendary\new\usersettings.php on line 725
Call Stack
#   Time    Memory  Function    Location
1   0.0042  880616  {main}( )   ..\usersettings.php:0

这是我当前的代码(包括不起作用的代码):

echo '
                    <table border="0" width="100%">
                    <tr><td style="font-size: 16px;">Change Image</td></tr>

                    <form action="" method="post" enctype="multipart/form-data">
                        <tr><td>Upload Image:</td><td style="text-align: right;"><input type="file" name="upimage" id="upimage" /></td></tr>
                        <tr><td></td><td style="text-align: right; font-size: 10px;"></td></tr>
                        <tr><td></td><td style="text-align: right;"><input type="submit" name="submitnewimage" value="Upload" class="button" /></td></tr>
                    </form>
                    ';

                    echo '
                    </table>
                    ';

                    if(isset($_POST['submitnewimage'])){
                        $name = $_FILES['upimage']['name'];
                        $temp = $_FILES['upimage']['tmp_name'];
                        $type = $_FILES['upimage']['type'];
                        $size = $_FILES['upimage']['size'];
                        if($name!=""){
                            include 'config.php';
                            $sql5 = mysql_query("SELECT * FROM images ORDER BY id DESC LIMIT 1");
                            while($row=mysql_fetch_array($sql5)) {
                                if(!isset($show2)){
                                    $id = $row['id'];
                                    $id = $id + 1;

                                    $show2 = "YES";
                                }
                            }

                            if(($type=="image/jpeg") || ($type=="image/jpg") || ($type=="image/gif") || ($type=="image/pjpeg") || ($type=="image/png")){
                                if($size<=100000){
                                    $pos = strrpos($name, '.');
                                    if($pos === false)
                                        $ext = "";
                                    $ext = substr($name, $pos);
                                    $newFilename = $id.$ext;

                                    move_uploaded_file($temp, "images/teamicons/".$newFilename);
                                    $im = new Imagick('images/teamicons/'.$newFilename); 
                                    $im->thumbnailImage(75,75); 
                                    $im->writeImage('images/teamicons/'.$newFilename);

                                    mysql_query("INSERT INTO `images`(`id`, `name`, `size`, `type`) VALUES (NULL,'$newFilename',$size,'$type')");
                                    $myusername = $_SESSION['myusername'];
                                    mysql_query("UPDATE `members` SET `img`= '$newFilename' WHERE `username`='$myusername'");

                                    header("Location:" . $_SESSION['prev_page']);
                                }else{echo "<tr><td colspan='2'><span style='color:#F00;'>The file, &quot;".$name."&quot;, is too large! Maximum allowed file size is 100kB.</span></td></tr>";}
                            }else{echo "<tr><td colspan='2'><span style='color:#F00;'>&quot;".$type."&quot; is not a valid file type!</span></td></tr>";}
                        }else{echo "<tr><td colspan='2'><span style='color:#F00;'>No file has been specified!</span></td></tr>";}
                    }

有没有办法改变图像的宽度和高度?

解决方法:

我最近成功使用了GD,特别是使用了imagecopyresampled函数.

稍微扩展一下…一旦我上传了图像(我不会进入,因为这是另一个问题),我做了一件相当简单的事情:

$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
$original_img = imagecreatefromjpg($filename);
$thumb_w = 100;
$thumb_h = 100;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($thumb_img, $original_img,
                   0, 0,
                   0, 0,
                   $thumb_w, $thumb_h,
                   $original_w, $original_h);
imagejpeg($thumb_img, $thumb_filename);
imagedestroy($thumb_img);
imagedestroy($original_img);

请注意,我还没有测试过这段代码.这是为了让您对我的方法有一个基本的了解.

标签:php,image-resizing,image-processing
来源: https://codeday.me/bug/20190929/1831579.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有