0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Ahh, so there's my problem. Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.