Upload Image though PHP, save Image path to MySQL - php

Sure this is a easy one for you experts, I`m trying to upload a image to the site and save the path to the database. If possible, how can i change the image size before saving?
Here is my script that handles to update query when updating:
<?php // Get Event ID
$update=$_GET['update'];
// Create Session
$event_name=$_POST['event_name'];
$event_description=$_POST['event_description'];
$event_date=$_POST['event_date'];
$event_time=$_POST['event_time'];
$event_cost=$_POST['event_cost'];
$event_image=$_POST['event_image'];
// Connection to MySQL Database.
include ('_includes/_dbconnection.php');
include ('_includes/_dbopen.php');
// Update Event using Event ID.
$sql="UPDATE b_events SET ename = '$event_name', edescription = '$event_description', edate = '$event_date', etime = '$event_time', ecost = '$event_cost', eimage = '$event_image' WHERE id = '$update'";
$result=mysql_query($sql);
if($result){
header('Location: _events.php');
}
else {
header('Location: _home.php');
}
?>
I have the following code in the update.html for the image:
<input name="event_image" type="file" />
Thanks!!!

To change the image size must resize. A function to create a copy with specific dimensions.
<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
?>
Source: http://php.net/manual/en/function.imagecopyresized.php

Php has GD library for working with images: function imagecopyresampled resize an image. Verify that GD is enalbed in you php config
http://us3.php.net/imagecopyresampled

Related

saving imagejpeg output the file

Is there something I am missing here? If I change imagejpeg($thumb, $newImage);
to imagejpeg($thumb); it echos a load of unreadable characters. The thumb image directory exists.
My ultimate aim is to copy an image at a lower quality.
$filename = $imageDirectory . '/1.jpg';
$percent = 0.5;
$newImage = $imageDirectory . '/thumbs/1.jpeg';
echo "image: <br>";
echo $filename;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $newImage);
UPDATE: I now realize that the second parameter must be an image location with the new name. So... I have redefined $newImage. The path is fine... if I upload an image named 1.jpg to that location manually it exists at that path.
You'll have to add the correct Content-Type header for the response if you want to output the image to the browser:
header('Content-Type: image/jpeg');
imagejpeg($yourimage);
Please check the first example in the documentation for more information. If you want to decrease the quality before outputting to your browser check the third example.

PHP image resize isn't working

I found the following script from PHP site to half the size of the image. I use the database to fetch the link of the image. Other things are working correctly which means there isn't any kind of error anywhere except this one.
echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);
"'>"
Error:
The whole page is destroyed and there is a broken link of an image.
Hope you guys will help me!
It works after you drop those lines:
echo "<img src='".// File and new size
"'>"
header does it for you, informs that there is jpg coming, no need to echo image tag.
The other solution is to remove this line:
header('Content-Type: image/jpeg');
And create new file, then use it as a source of an image:
// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";
Try this it works
<?php
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
echo "<img src='".imagejpeg($thumb)."'>";
?>

Error in displaying images after resizing in php

My image is not getting displayed after resizing it.
require('connect.php');
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
$imagename = $_FILES["image"]["name"];
// Get new dimensions
list($width, $height) = getimagesize($tmpName);
echo "orginal width".$width."<br/>";
echo "orginal height".$height."<br/>";
$new_width= $width * 0.5;
$new_height = $height * 0.5;
echo "new width".$new_width."<br/>";
echo "new height".$new_height."<br/>"; //Its working fine till here.
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($tmpName);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
echo "My image"."<br>";
echo '<img src="data:image/png|image/jpeg|image/gif;base64,' . base64_encode( $image_p ) . '" style="max-width:400px; max-height:300px;"/>';
/* This line is not able to display the image.
Also tried imagejpeg($image_p, null, 100);. Not working , but anyway i need to
display image alongwith other content so i have used echo. Not sure which
variable i should work with to display the image and how do i do it.*/
}
It seems to me that you are missing one step, encoding the image resource as a jpeg image (for example...).
You could output the result from something like imagejpeg to a file and just serve the file or you could capture its output using output buffering and serve it as you do now.

php resize blob image

I am echoing the data received from a blob column from mysql like this:
<?php
$con = mysql_connect("localhost","username","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
if(isset($_GET['imageid'])){
$img=$_GET['imageid'];
$sql="SELECT image FROM vrzgallery WHERE id=$img";
$que=mysql_query($sql);
$ar=mysql_fetch_assoc($que);
echo $ar['image'];
header('Content-type: image/jpeg');
}
?>
QUESTION: How can i reduce my image to say like 500px X 500px
It is really the bad idea to store images in DB, because they are too big, harder to maintain, harder to work with and so on. You should store only path to it or file name.
To resize image you may use PHP's GD library. Create it using imagecreatefromstring() and manipulate using imagecopyresized() or imagecopyresampled(). Example from manual:
// File and new size
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb);

Save an image Resized with PHP

I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038

Categories