Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Image resizing

images php coding issues or problems here.

Moderators: macek, egami, gesf

Image resizing

Postby darkapple » Mon Apr 04, 2011 2:28 am

Hi All,

not sure if this is an easy thing or not at just learning PHP, what i am looking to do is display a photo on a page (I can do that part :D ) that a user has uploaded (again i can do the upload bit), the problem i have is i need the image to be automaticly resized to fit in a particular part of the page, i can set the size of the image manualy and the image location can just be a variable but thats ok if users only upload landscape images if they upload a portrate image it puts the image out of shape, i was wondering what the code would be to automaticly resize, hope that makes sence, it doesnt need to be a thumnail that is clickable just display 1 photo that they upload.

Thanks in advance
darkapple
New php-forum User
New php-forum User
 
Posts: 19
Joined: Wed Feb 23, 2011 10:57 am
Location: Essex, United Kingdom

Re: Image resizing

Postby egami » Mon Apr 04, 2011 7:11 am

Smells alot like homework, but I'll get you in the right direction.

you need to have a multipart/form-data form

You will also need to know what your max height, and your max width shoudl be.

Then take a look at www.php.net/imagecopyresized or www.php.net/imagecopyresampled

These things might look a little confusing at first, but you're going to need to do some research on what it is you need.

Start by figuring out what the layout is..
basically, if width > height, the layout is landscape, and the other way around is rather obvious.

Then figure what you want the maximum size you want the picture to be, both in height and width.

then compare the size of the picture to the size that you consider max, and then divide max_w into the size of the picture to get the aspect ratio that you want.

ie..
$size = getimagesize($file); //www.php.net/getimagesize

This creates an array of width,height,mimetype and a few other things.. very useful.

so, in english (not code since I believe this to be homework) ..

if layout is landscape
then width = size[width] / max_w
else it will be height = size[height] / max_h
This will give you an aspect ratio..

Then you will divide the other dimensions accordingly

Then you'll create a blank image using www.php.net/createimagetruecolor with the new height and dimensions (this creates a picture resource)
Then you'll use imagecopyresized or imagecopyresampled to copy the original picture, albet resized into the new resource.
Then depending on whatever mimetype it was, ie.. png,gif,jpg|jpeg you'll use
imagejpeg() [or so on] to create and write the file to disk.

then, if you want the user to be able to see the picture they just uploaded..

create an HTML reference like this..

<img src="directory/picture.jpg?<?=date('U')?>" border="0">
(applying the unix datestamp will ensure that the browser reloads the picture every time)


There are tons of references on the net for doing this very thing, and if it is homework, I hope you don't pladgerize, because otherwise you won't learn anything.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Image resizing

Postby darkapple » Mon Apr 04, 2011 7:42 am

Hi mate,

Thanks for the info, just to clarify its not homework I am an old chap (35) teaching myself PHP for fun as a bit of a geek

Thanks again for the help
darkapple
New php-forum User
New php-forum User
 
Posts: 19
Joined: Wed Feb 23, 2011 10:57 am
Location: Essex, United Kingdom

Re: Image resizing

Postby egami » Mon Apr 04, 2011 8:05 am

HTML
index.php

Code: Select all
<html>
<
head><title>My Image Resizer</title></head>
<
body>
<
form name="resizeme" method="post" action="." enctype="multipart/form-data">
<
input type="file" name="uploaded_file">
<
input type="submit" name="action" value="Submit">
</
form>

<?
php
//Just off the top of my head
if ($_POST['action']) { 
  if (
is_uploaded_file($_FILES['uploaded_file'])) { 
    
$sizeofpic getimagesize($_FILES['uploaded_file']['tmp_name']); // it's a wierd funky name.. different than the actual name of the file
    
$tmpfile $_FILES['uploaded_file']['tmp_name'];
    
$filename strtolower($_FILES['uploaded_file']['name'];
    
$width $sizeofpic['0'];
    
$height $sizeofpic['1'];

    
$max_w 400;
    
$max_h 400;

    if (
$width $height) { 
      if (
$width $max_w) { 
        
$percentage $width $max_w;
      } 
    } else { 
      if (
$height $max_h) { 
        
$percentage $height $max_h;
      } 
    }

    if (!isset(
$percentage)) { 
      
$percentage 1;
    }

    
$new_w $width $percentage;
    
$new_h $width $percentage;

    
$newimage imagecreatetruecolor($new_w,$new_h);
    
    
$array = array ('jpg','jpeg','png','gif');
    
$ext  end(explode(".",strtolower($_FILES['uploaded_file']['name'])));
    if (
in_array($ext,$array)) { 
      switch (
$ext) { 
        case 
"jpg":
          
$source imagecreatefromjpeg($tmpfile);
          
imagecopyresized($newimage,$source,0,0,0,0,$new_w,$new_h,$width,$height);
          
imagejpeg($newimage,"uploads/$filename");
          break;

        
//... recreate the above for jpeg, png and gif..  replacing jpeg with png, and gif..
        
}
      echo 
'<img src="uploads/'.$filename.'?'.date('U').'" border="0">';
     } else { 
      echo 
"File extension not accepted.<br>";
     }
  }
}
 
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Image resizing

Postby egami » Mon Apr 04, 2011 8:06 am

ps.. I'm 40..
so I know what you're going through.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Image resizing

Postby egami » Mon Apr 04, 2011 8:10 am

ps..
I assumed you had a directory called uploads, and that it is read/writeable by the webserver.
if not, remove uploads reference, and keep it in the same directory to which you are working.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Image resizing

Postby darkapple » Mon Apr 04, 2011 9:18 am

thanks mate for that, to be honist i probably cheated a bit by using the getimagesize function and an if else statement :) and a echo for the image, but will have a play with yours as will probably be better :D
darkapple
New php-forum User
New php-forum User
 
Posts: 19
Joined: Wed Feb 23, 2011 10:57 am
Location: Essex, United Kingdom


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron