tags:

views:

202

answers:

3

my code basically should crop the image to 219px by 127px and save the image to the database but im getting errors and cant 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 dont see any errors but the crop isnt happending. do i have something wrong?

thnx

[updated code] here is the new peice of block for some reason the thumb wont 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());
A: 

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();

EFraim
your actually right but where would $canvas be put? in the $handle variable like this? $handle = fopen($canvas, "r"); that iddnt work and i dont see how it would grab an image like that. if u dont mind can you point it out for me :) im trying to figure out as of now also thnx.
tried that it doesnt want to work for some reasson :(
Please elaborate: What do you mean, it does not work?
EFraim
ive reposted the code above there
You must be saving to a write-protected directory. The current directory.
EFraim
You can also get away without temp file. See the updated answer.
EFraim
i got it to work like this: $content = addslashes(ob_get_contents()); thanks alot dude!!!!... but what does ob_start and ob_getcontents do? whats their purpose in this? thnx
They buffer the output stream until you do either flush or clean. imagepng without filename outputs to the output stream. Then you grab buffer's content and discard the buffer.
EFraim
A: 

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.

Ian Selby
A: 

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.

Stojg