Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Image Size - PHP

I have recently created an upload function, but I don't know how to change the width and height to 75px... I tried one code I found through Google, but I just got this error:

( ! ) 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

Here's my current code (including the code which didn't work):

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>";}
                    }

Is there any way of changing the width and height of the images?

like image 519
ZyteX Avatar asked Jan 23 '12 21:01

ZyteX


People also ask

How do I scale an image in PHP?

The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

How do I make an image smaller in PHP?

PHP in Telugu Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.

Can you change image size in HTML?

If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element.


1 Answers

I've successfully used GD to do this recently, specifically using the imagecopyresampled function.


To expand a little on that... Once I had the image uploaded (which I wont go into, because that is a whole other issue), I did something fairly simple like this:

$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);

Please note that I have not tested this code. It's just here to give you a basic idea of my method.

like image 113
Max Spencer Avatar answered Nov 14 '22 14:11

Max Spencer