Jump to content

[SOLVED] Auto picture resizing


smc

Recommended Posts

Hello,

I would like to add an upload function when submitting an article on my website that allows to upload a picture. Now this picture will need to be resized to a certain dimension for the physical article and another will need to be resized to be a thumbnail.

I've googled but not found a reliable source of information on how to resize a picture.

Thanks!!
Link to comment
Share on other sites

I could use a bit more guidence. I'm finding the provided examples a bit murky

My goal is to upload the image and as it's uploaded to resize it, then to copy it to a new directory with the name $articletitle . "_pic"  or $articletitle . "_thumb" so I can store the link to it and call the link from the database

Thanks
Link to comment
Share on other sites

This is a function and test script that I wrote a while back for a friend who wanted to do something similar:
[code]
<html><body>
<?php
function resize_jpg($inputFilename, $new_h, $new_w, $outputFilename){
echo $inputFilename;
$imagedata = getimagesize($inputFilename);
$w = $imagedata[0];
$h = $imagedata[1];
$im2 = ImageCreateTrueColor($new_w, $new_h);
$image = ImageCreateFromJpeg($inputFilename);
imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);
touch($outputFilename);
imagejpeg($im2, $outputFilename);
}

if (!($_POST)) {
?>
<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
else {
if ($_FILES['imagefile']['type'] == "image/jpeg") {
resize_jpg($_FILES['imagefile']['tmp_name'], 300, 300, "rox.jpg");
}
}

?>
</body></html>
[/code]

A problem you'll hit if you use the function as it is, is that it will fail if the file is already there. You can modifiy it to delete, or rename.

But, hopefully you won't just copy and paste it, and this will help you understand how the functions work, and what functions to use.
Link to comment
Share on other sites

Yes. Most functions return a FALSE bool when they fail. So stuff like:
[code=php:0]
if(!$imagedata = getimagesize($inputFilename)) die('WTF are you trying to upload??');
[/code]

And it will still set $imagedata correctly, but can be used for error handling of sorts.
Link to comment
Share on other sites

When attempting to use your function I get this

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/een/public_html/functions.php on line 205

Nothing I adapted deals with that function so I'm not sure

EDIT: To help further here are specific instances

functions.php
[code]
function resize_jpg($inputFilename, $new_h, $new_w, $outputFilename){
// chmod("images", 777);
echo $inputFilename;
$imagedata = getimagesize($inputFilename);
$w = $imagedata[0];
$h = $imagedata[1];
$im2 = ImageCreateTrueColor($new_w, $new_h);
$image = ImageCreateFromJpeg($inputFilename);
imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);
touch($outputFilename);
imagejpeg($im2, $outputFilename);
// chmod("images", 755);
}
[/code]

actual doc calling it
[code]
if ( $picture != "" || $_FILES['picture']['type'] == "image/jpeg" ){
$outputFilename = $titledb . "_pic.jpg";
$inputFilename = $_FILES['picture']['tmp_name'];
resize_jpg($inputFilename, 240, 340, $outputFilename);
$picturedb = "http://eagleeye5.com/images/" . $outputFilename;
}
else{
$picturedb = "false";
}
[/code]
Link to comment
Share on other sites

I was actually using the wrong name after all. I fixed it and it no longer generates errors but unfortunatly it doesn't echo the input file eithier. Any ideas?

Also, if anyone has any other methods I'm open to hear them. I'm stuck on this issue
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.