Uploading to mySQL and using SimpleImage PHP? - php

I'm hoping this is an easy one, I'm using mySQL and php to upload an image as a BLOB type using this code:
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
This is all working fine, inserting to the database and everything. I then want to use SimpleImage: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ to perform some resize and compression work on the image before uploading it. I can't see how to combine say:
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['userfile']['tmp_name']);
$image->resizeToWidth(150);
$image->output();
With my existing code, I think what I want to do is get $content to become $image, but I've tried for a while and can't find a way of doing it. Any help much appreciated.
Happy Christmases to those who like Christmas and TIA to all.

You are outputting the resized image to the browser (::output()) but you're not saving it. If you want to store it inside the database, you need to change the temporary file first, e.g. by using the ::save() function of SimpleImage.
Next to that, you write you want to resize the picture in the browser before uploading. That can not be done with PHP, but only with javascript and browsers who support that. Additionally the upload handling on the PHP side might differ then. But I'm not really sure if you really meant it that the image is resized before uploading.
Another Idea I had is using an output buffer:
if (isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$image = new SimpleImage();
$image->load($_FILES['userfile']['tmp_name']);
$image->resizeToWidth(150);
ob_start();
$image->output();
$content = ob_get_clean();
$content = addslashes($content);
...

Related

Type file (PNG or JPEG)

function upload(&$model){
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$file = $_FILES['file'];
$file_name = $file['name'];
$file_ext2 = explode('.', $file_name);
$file_extenstion = strtolower(end($file_ext2));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo(finfo_file($finfo, $file['tmp_name']));
echo($file['type']);
}
return "superliga_view";
}
Why
echo(finfo_file($finfo, $file['tmp_name']));
prints image/jpeg, but
echo($file['type']);
prints "image/png"?
I try make watemark, but
$image = imagecreatefrompng($target);
sometimes throws errors due to wrong file type. With small files php always reads them as png, but with large files it reads them as jpeg.

PHP image compression and uploading to mysql

I have a php script, which uploads pictures to a mysql database. The images are taken within the browser. I would like to compress them before uploading, but I'm not quite sure how exactly to compress the uploaded data. What I've got for the moment is this:
if(isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0)
{
//$positiony = $_POST['posy'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
$content = imagejpeg($content,null,50);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (team_name, id, display, content) ".
"VALUES ('$team_name', 'null', '1', '$content')";
mysql_query($query) or die('Error, query failed'.mysql_error());
echo "<br>File $fileName uploaded<br>";
}
The image uploading works fine, but the uploaded images are broken. Introducing imagejpeg as a form of compressing has caused the issues. Should I be using it on something else?
Most images are already compressed so there is no need to "compress them further".
Storing them in a database is not a recommended thing to do. Just upload them to a location on the server and save the path to that location.

Restricting all but one file format to be uploaded

I'm studying on MySQL & PHP, and for my first production I've started to work on a review panel. You can simply upload your product reviews to the database and browse them later on, directly from the panel, which in this case is a local website.
The problem is, I can't figure out how to rule over every file format on upload, except .pdf! To be more clear: I only want my upload form to accept .pdf files to be uploaded. At the moment it doesn't restrict anything, here is my code:
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$revName = $_POST['revname'];
$revRating = $_POST['rating'];
$revRecommend = $_POST['recommend'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
rename($tmpName,"C:\\xampp\\htdocs\\ReviewArchieve\\files\\reviews\\".$fileName);
include 'include/config.php';
include 'include/opendb.php';
$query = "INSERT INTO files (revname, rating, recommend, name, size, type, content)".
"VALUES ('$revName', '$revRating', '$revRecommend', '$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed'.mysql_error());
include 'include/closedb.php';
echo "<br>File $fileName uploaded<br>";
}
?>
Got it working!
Thanks to the MIME refer, I managed to learn something new, and accomplished my task with a little bit of investigation! It was not the part of code offered in the correct answer, that did not work at all in my case, no matter what I did, but instead, I used this method:
I noticed I have already included the file type.
$fileType = $_FILES['userfile']['type'];
So now I just had to make an if from it, like this:
if($fileType == 'application/pdf') {
*** Code to be driven here, same as above on the original code ***
}
else {
echo "Invalid file, upload interrupted!";
}
Answer:
....
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$tmpName = $_FILES['userfile']['tmp_name'];
if (mime_content_type($tmpname) != 'application/pdf') {
die("uploaded file not valid");
}
....
You have a number of problems here the biggest are:
SQL Injection. You must sanitize your user inputs or little bobby tables will visit you. Think about using parametrized queries
You should check the file's mimetype.
http://www.php.net//manual/en/function.mime-content-type.php works out the box although is deprecated. You should use fileinfo.

PHP $_FILES Array Manipulation

I am using a form to upload an image. Upon image upload, we will be able to see the uploaded image. Then I used JCrop (http://deepliquid.com/content/Jcrop.html) to allow cropping for this image. Let's assume I only care about JPEG images. I am then ready to submit the form.
Upon form submission I will perform some image manipulation and crop the image. However I want to put the information for this cropped image back into the $_FILES array (this is a must). How would I go about manipulating this $_FILES array in a PHP script?
Here is what I had attempted and it would not work.
$upload_dir = '/Users/user/Sites/tmp/';
$file_name = $_FILES['image']['name'];
$file_name = "cropped_".$file_name;
$tmp_name = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$src_file = imagecreatefromjpeg($tmp_name);
list($width,$height) = getimagesize($tmp_name);
// Creates cropped image
$tmp = imagecreatetruecolor($_POST['w'], $_POST['h']);
imagecopyresampled($tmp, $src_file, 0, 0, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'], $_POST['w'], $_POST['h']);
$small_pic_file_path = $upload_dir.$file_name;
imagejpeg($tmp,$small_pic_file_path,85);
$message = "<img src='http://localhost/~user/tmp/".$file_name."'>";
$_FILES['image']['name'] = $file_name;
$_FILES['image']['type'] = "image/jpeg";
// unlink($tmp_name);
// if(!move_uploaded_file($upload_dir.$file_name, $tmp_name)) echo "Failure Moving Image";
$_FILES['image']['tmp_name'] = $upload_dir.$file_name;
$_FILES['image']['error'] = 0;
$sizes = getimagesize($upload_dir.$file_name);
$_FILES['image']['size'] = ($sizes['0'] * $sizes['1']);
It is allowable to change this $_FILES array?
Only changing in the global array won't work. You are just saying $_FILES['image']['tmp_name'] will be the newly created one. But the tmp location is having same old file.
But you have to update the image in /tmp folder.
Get your tmp path and copy the image there.
kind of copy($newImage, /tmp) - get your tmp folder path from php.ini
It's not a good idea to change a superglobal you'd better copy it into another variable and after do whatever you want.

image crop with gd then upload to db but not resizing

My code basically should crop the image to 219px by 127px and save the image to the database but I'm getting errors and can't figure it out.
<?php
if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {
$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);
//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);
$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = addslashes($content);
fclose($handle);
$save = mysql_query("insert into tbl_school_preview values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
//header("Location: school-catalog.php?page=school_preview");
}
?>
I don't see any errors but the crop isn't happening. Do I have something wrong?
[Updated code]
Here is the new piece of block for some reason the thumb won't save.
if(isset($_POST['btnupload']) && $_FILES['imglogo']['size'] > 0) {
//$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $_FILES['imglogo']['tmp_name'];
$imgsize = $security->secure($_FILES['imglogo']['size']);
$imgtype = $security->secure($_FILES['imglogo']['type']);
$school = $security->secure($_POST['school']);
//crop image
$canvas = imagecreatetruecolor(219,127);
$imgattrib = getimagesize($tmpname);
$source = imagecreatefromjpeg($tmpname);
imagecopyresized($canvas,$source,0,0,0,0,219,127,$imgattrib[0],$imgattrib[1]);
$newfile = imagejpeg($canvas,'thumb.jpg',100);
$handle = fopen($newtmpfile, "r");
$content = fread($newtmpfile, filesize($newtmpfile));
$content = addslashes($content);
fclose($handle);
$save = mysql_query("insert into tbl_school_preview values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
You don't need to do any fopen / fwrite stuff to save your image. Replace all that with something like:
$filename = '/path/to/desired/save/location.png'
imagepng($canvas, $filename);
And you should be good to go. The imagepng function will write the file when you give it the second parameter. Same would be true for imagegif and imagejpeg.
You should save the content of $canvas, not the content of the original image.
Output it to temporary file with imagepng, or directly output stream, which you will then have to intercept.
ob_start();
imagepng($canvas);
$out = ob_get_contents();
ob_end_clean();
Just two tips:
I would first of all not to save the file to the filesystem.. You can use ob_start() to get the content of the file.
ob_start();
imagepng($canvas);
$imageString = ob_get_contents();
ob_end_clean();
$save = mysql_query("
insert into tbl_school_preview
values(null,'$school',$imagestring,'$imgtype','$imgsize')
")or die(mysql_error());
Also use imagecopyresampled() for better quality of the thumbnail. Beware of strange characters in the image code when inserting into the database.

Categories