Forum Moderators: coopster

Message Too Old, No Replies

Resizing images

One image three sizes and diffrent folders

         

Malmo

8:21 am on Aug 10, 2010 (gmt 0)

10+ Year Member



Hi guys!

I am still a noob when it comes to php and I am trying to do a script that works like this: When I upload an image I need it to resize three times and in separate folders. The upload code is created and working it pasted below, but I can´t figure out how to resize the images, I have checked the php manual, but for some reasons it doesn˝t work for me, perhaps because it is not setup right. I thank everyone in advance for the help you will give me.

<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="datoteka" value="išči"> <input type="submit" value="naloži">
</form>

<?php


@$datoteka = $_FILES['datoteka']['name'];
@$tmp = $_FILES['datoteka']['tmp_name'];

@$ext = substr("$datoteka", -3);

if(!empty($datoteka))
{
if($ext == "jpg")
{
move_uploaded_file($tmp, "datoteke/slike/$datoteka");
}
else
{
move_uploaded_file($tmp, "datoteke/$datoteka");
}
echo "Datoteka <b>$datoteka</b> uspešno naložena!";
}

?>


Best regards from Sweden

morehawes

9:30 am on Aug 10, 2010 (gmt 0)

10+ Year Member



Hi, what PHP errors are you getting? If you are getting a blank screen then insert this after the first opening <?php at the top of your script :


error_reporting(E_ALL);
ini_set("display_errors", 1);

Matthew1980

9:41 am on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Malmo,

Lol, we're all noob's really, just at different stages ;-p

Be careful when using images on php, ensure that both your development setup & you host server support the use of image functions (GD library plugins)

Only thing I will remark on with your code as you have posted is this:-

@$datoteka = $_FILES['datoteka']['name'];
@$tmp = $_FILES['datoteka']['tmp_name'];

@$ext = substr("$datoteka", -3);

There is no need to suppress any error messages at this point, however there are some functions that php themselves recommend you do this for, but off hand I can't recall what they are, surely though you would rather know what errors there are so that you can code them out, or investigate why they are being flagged up.

Use error_reporting(E_ALL); at the top of the script to try and eradicate any warnings or notices that are raise by the parser.

I have started to use: error_reporting(E_ALL|E_DEPRECATED) so that you can see if you need to use alternative functions for example preg_ instead of ereg_, as anything ereg/eregi is now not supported by newer versions of php.

Also this:-
>>@$ext = substr("$datoteka", -3);

is already available in the $_FILES array as $_FILES['datoteka']['type']; if you echo this it will give you the mime type of the file being uploaded... [php.net ]

Hope that makes sense to you anyway ;-p

Cheers,
MRb

Malmo

11:57 am on Aug 10, 2010 (gmt 0)

10+ Year Member



thanks for the answers, but the main question is still open and that is how to make the resizing. To be proper I need an image to be 200px * 300px, 75px * 50px and 400px * 300px.

I have figured out how to resize an image but with png format and not jpeg like I need. If I put image.jpeg in won´t work. Does this mean that I need this code on every page that I need the image or is there a option so the image is really cut in the dimension that I need and I only call the folder.

<?php
header("Content-type: image/jpeg");
$ime="upload/datoteke/slike/image.png";
$podatki=getimagesize($ime) or die("$ime ni slika!");
switch($podatki[2])
{
case 1: $img=imagecreatefromgif($ime);
case 2: $img=imagecreatefromjpeg($ime);
case 3: $img=imagecreatefrompng($ime);
}
$sirina = $podatki[0];
$visina = $podatki[1];

$nova_sirina=75;
$razmerje=$nova_sirina/$sirina;

$im=imagecreatetruecolor($nova_sirina,floor($visina*$razmerje));
imagecopyresampled($im,$img,0,0,0,0,$nova_sirina,floor($visina*$razmerje),$sirina,$visina);

imagejpeg($im);
imagedestroy($im);
?>

Matthew1980

12:07 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there malmo,

switch($podatki[2])
{
case 1: $img=imagecreatefromgif($ime);break;
case 2: $img=imagecreatefromjpeg($ime);break;
case 3: $img=imagecreatefrompng($ime);break;


You were missing the breaks off, unless you want everycase to create all three image types ;)

Cheers,
MRb

morehawes

12:10 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



error_reporting(E_ALL|E_DEPRECATED)


Good shout MRb - I shout probably start doing this!

If I put image.jpeg in won´t work.


Maybe the errors will give you a clue?