Reputation: 2843
I saw this script on a site, and tried to get it working with my code. But when i upload the image i get nothing echo'ed out, and my image is not resized. I hope someone could see what im doing wrong.
class.imageResizer.php
<?php
class ImgResizer {
var $originalFile = '$newName';
function ImgResizer($originalFile = '$newName') {
$this -> originalFile = $originalFile;
}
function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 95);
}
}
?>
The uploader
//If no errors do this
if (isset($_POST['Submit']) && !$errors)
{
//Resizing the picture
include 'class.imageResizer.php';
$work = new ImgResizer('users/$username/$imageName');
$work -> resize(400, 'users/$username/$username-246.$extension');
$sql = "UPDATE members SET user_pic='http://www.something.net/$newName' WHERE username='$username'";
$_SESSION['user_pic'] = $newName;
$result = mysql_query($sql);
mysql_close($conn);
}
The $newName = users/theusername/theusername.theextension
The $imageName = theusername.theextension
Upvotes: 0
Views: 2896
Reputation: 7603
Aside from having file permission issues, one of the issues here is the single quotes.
http://php.net/manual/en/language.types.string.php
Instead of this,
$work = new ImgResizer('users/$username/$imageName');
$work -> resize(400, 'users/$username/$username-246.$extension');
Change it to
$work = new ImgResizer("users/$username/$imageName");
$work -> resize(400, "users/$username/$username-246.$extension");
Or to make it more predictable / readable
$work = new ImgResizer("users/".$username."/".$imageName);
$work -> resize(400, "users/".$username."/".$username."-246.".$extension);
Same goes for this line,
var $originalFile = '$newName';
function ImgResizer($originalFile = '$newName') {
Either drop the single quotes, or change it to double quotes.
Upvotes: 1
Reputation: 10247
Well for one thing you are placing variables in single quotes where you should be using double quotes. The variables are read as text rather than values:
Wrong:
$work = new ImgResizer('users/$username/$imageName');
Right:
$work = new ImgResizer("users/$username/$imageName");
Upvotes: 0