Forum Moderators: coopster

Message Too Old, No Replies

Image resize not working

PHP image resize script not working

         

jspeed

2:00 am on Jul 27, 2010 (gmt 0)

10+ Year Member



I am using swfupload to upload images to my server. The upload works fine - its the image resizing script that I can't get to work.

Its like the upload page ignores the fact that the resize script is there.


--------------------------
^^^ Script that gets image from previous page ^^^
--------------------------
if (!move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {
HandleError("File could not be saved.");
exit(0);
}

if (!copy($save_path.$file_name,"uploads/thumb/".$file_name)){
HandleError("File could not be copied.");
exit(0);
}
// AT THIS POINT SCRIPT WORKS - SAME IMAGE IN TWO DIFFERENT DIRECTORIES

class ImgResizer {
private $originalFile = 'uploads/thumb/{$file_name}';
public function __construct($originalFile = 'uploads/thumb/{$file_name}') {
$this -> originalFile = $originalFile;
}
public 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, 85);
}
}
$work = new ImgResizer('uploads/thumb/{$file_name}');
$work -> resize(150, 'uploads/thumb/{$file_name}');

if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}

$file_id = md5(rand()*10000000);

$_SESSION["file_info"][$file_id] = $file_name;

echo "FILEID:" . $file_id;// Return the file id to the script


Any ideas? Or am I doing something ignorantly obvious? If I run just the resize script it works great.

jspeed

3:23 am on Jul 27, 2010 (gmt 0)

10+ Year Member



I think it has something to do with the variable $file_name within the ImgResizer class. How do I pass an "external" variable into the class?

jspeed

4:37 am on Jul 27, 2010 (gmt 0)

10+ Year Member



Not very familiar with classes, but anyone interested in the solution, I was able to pass the variable through the constructor:

$img = "uploads/thumb/".$file_name; // DECLARE IMAGE PATH

class ImgResizer {

private $originalFile;
function __construct($img) {
$this -> originalFile = $img;
}
public 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, 85);
}
}
$work = new ImgResizer($img);
$work -> resize(150, $img);

I am not sure as the etiquette for links, but credit for the class: www.kavoir.com/2009/01/php-resize-image-and-store-to-file.html [kavoir.com]