Reputation: 191
I am uploading image with form like this:
include 'extraphp/config.php';
$title = $_POST['titleStory'];
$excerpt = $_POST['excerptStory'];
$story = $_POST['storyStory'];
$catagory = $_POST['catagory'];
$tags = $_POST['tagsStory'];
$author = $_POST['authorStory'];
$date = $_POST['dateStory'];
/* Image Upload */
$image = $_FILES["imageStory"];
$image2= preg_replace("/[^A-Z0-9._-]/i", "_", $image["name"]);
$target_dir = "../uploads/large/";
$target_file = $target_dir . $image2;
move_uploaded_file($_FILES['imageStory']['tmp_name'], $target_file);
$catagory2 = implode (", ", $catagory);
$author2 = implode(',', $author);
$sql="INSERT INTO story (titleStory,excerptStory,storyStory,catagory,tagsStory,authorStory,dateStory,imageStory) VALUES ('". $title ."','". $excerpt ."','". $story ."','". $catagory2 ."','". $tags ."','". $author2 ."','". $date ."','". $target_file ."')";
mysqli_query($con,$sql);
header("location: dashboard.php")
now I want to resize this image to 180px X 109px.
Any body help me?
Upvotes: 0
Views: 179
Reputation: 91
Remember that you need to check the image extension in order to use the corret imagecreate...
funtion. The example is using always imagecreatefromjpeg()
assuming that all uploaded images are JPG.
list( $imageWidth, $imageHeight ) = getimagesize( $target_file );
$resampledImage = imagecreatetruecolor( 180, 109 );
//Check file extension here to use the correct image create function
//imagecreatefromjpeg(); imagecreatefrompng(); imagecreatefromgif() etc...
$source = imagecreatefromjpeg( $target_file );
imagecopyresized( $resampledImage, $source, 180, 109, $imageWidth, $imageHeight );
ob_start();
//Check file extension here to use the correct image output function
//imagejpeg(); imagegif(); imagepng() etc...
imagejpeg( $resampledImage, null, 100 );
$imageContent = ob_get_clean();
file_put_contents( $target_file, $imageContent );
http://php.net/manual/en/function.getimagesize.php
http://php.net/manual/en/function.imagecreatetruecolor.php
http://php.net/manual/en/function.imagecreatefromjpeg.php
http://php.net/manual/en/function.imagecreatefrompng.php
http://php.net/manual/en/function.imagecopyresized.php
http://php.net/manual/en/function.imagejpeg.php
http://php.net/manual/en/function.ob-start.php
http://php.net/manual/en/function.file-put-contents.php
Upvotes: 2