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
https://forums.phpfreaks.com/topic/35986-solved-auto-picture-resizing/
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
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.
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.