| PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >> |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread |
|
|
#1 (permalink) |
|
Registered User
Seasoned Poster
Joined in Oct 2003
41 posts
Gave thanks: 0
Thanked 2 times
|
imageMagick help
I am currently working on writing an image upload script for my website.
I have gotten the whole thing working, except I need to be able to automatically resize and optumise the images that people upload. I have been able to accomplish this with GD, but the image quality afterwards was terrible. The current function I have for saving the images can be seen below. The problem with it is that instead of saving the new image from the old image, it turns the old image into nothing and doesn't write a new one. (Not the desired action )function imageUploader($imageName,$width=640,$height=480) { //********************** Verify mime types and determine file extension *********************** if($_FILES[$imageName]['type'] == "image/pjpeg" || $_FILES[$imageName]['type'] == "image/jpeg") { $ext = '.jpg'; }elseif($_FILES[$imageName]['type'] == "image/x-png" || $_FILES[$imageName]['type'] == "image/png") { $ext = '.png'; }elseif($_FILES[$imageName]['type'] == "image/gif") { $ext = '.gif'; //convert .gif to jpeg! } else return false; //***************************** Do Filename ************************** $badString = array (1,2,3,4,5,6,7,8,9,0,'!',',','-','/',':','_',' ','thumb','.','jpg','png','jpeg','gif','bmp','swf' ); $fileName = str_replace($badString, '', strtolower($_FILES[$imageName]['name'])); // Remove Nonsense $fileName = str_replace(" ", "_", $fileName); // Convert Spaces $fileName = '/uploads/images/'.$_SESSION['userId'].$fileName; while(file_exists($_SERVER['DOCUMENT_ROOT'].$fileName.$ext) || file_exists($_SERVER['DOCUMENT_ROOT'].$fileName.'-original'.$ext)) { //make sure we are not overwriting other files... $fileName .= chr(mt_rand(97,122)); //add random lowercase letter to filename } $temporaryName = $fileName.'-original'.$ext; $fullTemporaryName = $_SERVER['DOCUMENT_ROOT'].$temporaryName; $thumbName = $fileName.'-thumb'.$ext; $fullThumbName = $_SERVER['DOCUMENT_ROOT'].$thumbName; $fileName .= $ext; $fullFileName = $_SERVER['DOCUMENT_ROOT'].$fileName; if(!$_FILES[$imageName]['tmp_name'] || !$_FILES[$imageName]['size']) return 'No File to Upload!'; if (!move_uploaded_file($_FILES[$imageName]['tmp_name'], $fullTemporaryName)) { return 'Unknown Error.'; } //********************** GENERATE THUMBNAIL IMAGE ********************* $command = '/usr/bin/convert -size 100x100> '.$fullTemporaryName.' -thumbnail 100x100> -quality 80% '.$fullThumbName; exec($command); //************************* GENERATE FULL IMAGE *********************** $command = '/usr/bin/convert -size '.$width.'x'.$height.'> '.$fullTemporaryName.' -resize '.$width.'x'.$height.'> -quality 70% '.$fullFileName; exec($command); //************************* return The Value!!! *********************** if(is_readable($fullFileName) && is_readable($fullThumbName)) { //ImageMagic Worked! unlink($fullTemporaryName); //Remove temporary file return $fileName; } else { return $temporaryName; //IM Didn't work... just give 'em the original image ![]() } } Everything I have been reading says this should work, but it doesn't which leads me to think it may be a configuration problem on my server? Sorry to bug everyone about this, but I have been working on it several days and it's driving me crazy ![]() P.S. Just for the record, the site I am trying to get this working on is http://www.allthingsinteresting.com |
|
|
|
|
|
#2 (permalink) |
|
Surpass Fan
Super #1
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
|
using the PHP image functions would be easier for this. Reference: http://php.net/image
As for your function, it should go a little something like this: PHP Code:
__________________
- Evan Charlton | [site] | Server - SH58 |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Seasoned Poster
Joined in Oct 2003
41 posts
Gave thanks: 0
Thanked 2 times
|
It Works!
Thanks for your reply!
Following is the new image upload function. I have rewritten it according to your suggestions. I would rather have this working with imagemagick, but since it works I can't complain! ![]() PHP Code:
Try inserting pictures into the textarea! |
|
|
|