Jump to content

imgrooot

Members
  • Content Count

    321
  • Joined

  • Last visited

  • Days Won

    1

imgrooot last won the day on May 16 2018

imgrooot had the most liked content!

Community Reputation

1 Neutral

About imgrooot

  • Rank
    Advanced Member

Recent Profile Visitors

2,053 profile views
  1. I just realized what the issue was. The votes in the table have a different user_id than what I am currently logged in as. So naturally it will not return any results. Here's a new code based on your suggestion. Works fine. $count_votes = $db->prepare("SELECT SUM(votes) FROM entries WHERE user_id = :user_id"); $count_votes->bindParam(':user_id', $user_id); $count_votes->execute(); $get_votes = $count_votes->fetchColumn();
  2. I have a table with "votes" column. I basically want to retrieve the sum of all the votes columns associated with a user. This is the full code. It gives me "NULL" output. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries WHERE user_id = :user_id"); $count_votes->bindParam(':user_id', $user_id); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes; But if I remove the user_id bind paramter, it gives me the results. But I need that parameter to show which user has how many votes. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries"); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes; What am I doing wrong?
  3. Got it. Here is the updated form. It seems to work fine. // Message ID being an ID of individual message rows retrived from the DB. $message_id if(isset($_POST['submit']) AND $_POST['message-id'] == $message_id) { $post_message = trim($_POST['message']); $errors = array(); $db->beginTransaction(); if(empty($post_message)) { $errors[] = 'The message field can not be empty!'; } else { // ADD CODE HERE } if(empty($errors)) { $db->commit(); echo 'success'; } else { $db->rollBack(); } } <form action="" method="post"> <fieldset> <textarea name="message" maxlength="10000" placeholder="What would you like to say?"></textarea> </fieldset> <fieldset> <input type="hidden" name="message-id" value="<?php echo $message_id; ?>" /> <input type="submit" name="submit" value="Submit" /> </fieldset> </form>
  4. I am creating a user inbox system. I am retrieving all the unread messages. Each message row contains a "reply" form. So say I have 10 messages showing on a single page. That's 10 forms. What I would like to know is how can I submit any one of the 10 forms and not have it affect the remaining 9 forms? Here is the basic code. if(isset($_POST['submit'])) { $post_message = trim($_POST['message']); $errors = array(); $db->beginTransaction(); if(empty($post_message)) { $errors[] = 'The message field can not be empty!'; } if(empty($errors)) { $db->commit(); echo 'success'; } else { $db->rollBack(); } } <form action="" method="post"> <fieldset> <textarea name="message" maxlength="10000" placeholder="What would you like to say?"></textarea> </fieldset> <fieldset> <input type="submit" name="submit" value="Submit" /> </fieldset> </form>
  5. Good to know. I will do that from now on.
  6. That makes sense. I reversed the order and it seems to work fine now. Yes project_id is an auto-increment primary key field. I honestly never had issues with this method before so I kept using it. But I actually do have "date_created" with each record. So I can "ORDER BY date_created" if need be.
  7. I have a basic query where I am retrieving project records. I basically want to show the records from newest to oldest, but I also want to show the Featured projects first. Each Featured project is marked by "1" in the mysql table column. If it's not featured, then it's marked "0". $find_records = $db->prepare("SELECT * FROM projects ORDER BY project_id DESC, featured DESC LIMIT 10"); $find_records->execute(); $result_records = $find_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0) { foreach($result_records as $row) { } } The issue with above query is that the ORDER BY works only for the first component(project_id) and ignores the second component(featured). Do you see what's wrong with it?
  8. This is my first time working with ADF. I've been researching and found this link. http://adfxml.info/adf_spec.pdf Apprantly ADF uses XML to format data. So say I have a car dealer that wants to receive leads in ADF format like this example. How do I go on about emailing this as an attachment? If I am using a PHPmailer, it requires a physical file to attach. So how do I save the bottom xml info as an attachment? Which file extension do I use? <?ADF VERSION "1.0"?> <?XML VERSION “1.0”?> <adf> <prospect> <requestdate>2000-03-30T15:30:20-08:00</requestdate> <vehicle> <year>1999</year> <make>Chevrolet</make> <model>Blazer</model> </vehicle> <customer> <contact> <name part="full">John Doe</name> <phone>393-999-3922</phone> </contact> </customer> <vendor> <contact> <name part="full">Acura of Bellevue</name> </contact> </vendor> </prospect> </adf>
  9. I am back and I have solved the issue. I just had to move up the $exif code before the orientation switch statement. It rotates the jpeg images now. There is a slight problem however. I'm not sure if it's normal or not. But if I upload a png or gif image, I get this error Warning: exif_read_data(phpA107.tmp): File not supported in 1. It uploads the png and gif images fine. 2. It doesn't compress gif and png images. Only jpegs are compressed. 3. Do I need to apply orientation code to the gif and png images in the function or only to jpeg? Having said that here's the new code. function compressImage($source_url, $destination_url, $quality) { //$quality :: 0 - 100 $info = getimagesize($source_url); $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } if($destination_url == NULL || $destination_url == "" ) { $destination_url = $source_url; } else if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagejpeg($image, $destination_url, $quality); //Free up memory imagedestroy($image); } else if($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagegif($image, $destination_url, $quality); } else if($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imageAlphaBlending($image, true); imageSaveAlpha($image, true); /* chang to png quality */ $png_quality = 9 - round(($quality / 100 ) * 9 ); imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression). //Free up memory imagedestroy($image); } else { return FALSE; } return $destination_url; }
  10. Alright after taking some time off, here is the reworked function. Its' combining compression and orientation. It no longer gives me any errors. But it's still not rotating the image when uploaded. Can you spot what's wrong with the code? function compressImage($source_url, $destination_url, $quality) { //$quality :: 0 - 100 $info = getimagesize($source_url); if($destination_url == NULL || $destination_url == "" ) { $destination_url = $source_url; } else if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } //save file //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75). imagejpeg($image, $destination_url, $quality); //Free up memory imagedestroy($image); } else if($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imageAlphaBlending($image, true); imageSaveAlpha($image, true); if(isset($orientation)) { switch($orientation) { case 8: $image = imagerotate($image, 90, 0); break; case 3: $image = imagerotate($image, 180, 0); break; case 6: $image = imagerotate($image, -90, 0); break; } } $exif = exif_read_data($source_url); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; } /* chang to png quality */ $png_quality = 9 - round(($quality / 100 ) * 9 ); imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression). //Free up memory imagedestroy($image); } else { return FALSE; } return $destination_url; }
  11. I am trying but I keep getting same errors. Here is the combined function of compression and rotation. function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg') { $image = imagecreatefromjpeg($source_url); } else if ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source_url); } else if ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); } else { $image = null; } $new_image = imagecreatefromstring(file_get_contents($image)); $exif = exif_read_data($image); if(!empty($exif['Orientation'])) { switch($exif['Orientation']) { case 8: $new_image = imagerotate($new_image,90,0); break; case 3: $new_image = imagerotate($new_image,180,0); break; case 6: $new_image = imagerotate($new_image,-90,0); break; } } imagejpeg($new_image, $destination_url, $quality); return $destination_url; }
  12. Here's what I have tried. It's still not working. I get the following errors. Warning: getimagesize() expects parameter 1 to be string, resource given in Warning: rename(XfdpPCBxns.jpg,../members/images/7/projects/8/): The system cannot find the file specified. (code: 2) in // Also if I var dump the $oriented_image variable in the code below, I get the following output. resource(92) of type (gd) // ORIGINAL CODE WITHOUT THE ORIENTATION $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($source_file, $new_image, 50); $new_file_path = $target_dir . $resized_image; $check = getimagesize($source_file); if(rename($new_image, $new_file_path)) { echo 'success'; } // NEW CODE WITH THE ORIENTATION FUNCTION $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $oriented_image = getOrientedImage($source_file); $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($oriented_image, $new_image, 50); $new_file_path = $target_dir . $resized_image; $check = getimagesize($oriented_image); if(rename($new_image, $new_file_path)) { echo 'success'; }
  13. So whether I am uploading an image through my iphone or sending that image to my computer and uploading it from the computer, it has the same effect. If I upload that image, it'll orient the image in landscape mode. Having said that, I found a function that can fix the orient issue. The problem is I don't know the proper way to integrate it into the image upload script. I have tried several different ways but they all give me errors. Can you show me where exactly I should use this function? // IMAGE ORIENTATION function getOrientedImage($imagePath) { $image = imagecreatefromstring(file_get_contents($imagePath)); $exif = exif_read_data($imagePath); if(!empty($exif['Orientation'])) { switch($exif['Orientation']) { case 8: $image = imagerotate($image,90,0); swapHW(); break; case 3: $image = imagerotate($image,180,0); break; case 6: $image = imagerotate($image,-90,0); swapHW(); break; } } return $image; } // IMAGE UPLOAD SCRIPT if(isset($_FILES['fileToUpload']) AND !empty($_FILES['fileToUpload']["name"])) { if(is_uploaded_file($_FILES['fileToUpload']["tmp_name"])) { $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compressImage($source_file, $new_image, 50); $new_file_path = $target_dir . $resized_image; if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } $uploadOk = 1; // Check if image file is a actual image or fake image $check = getimagesize($source_file); if($check !== false) { // echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $errors[] = 'File is not an image!'; $uploadOk = 0; } // Check if file already exists if (file_exists($target_file)) { $errors[] = 'Sorry, file already exists!'; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { $errors[] = 'Sorry, your file size is bigger than 5mb!'; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") { $errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed!'; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if($uploadOk == 0) { $errors[] = 'Sorry, your file was not uploaded!'; // if everything is ok, try to upload file } else { if(rename($new_image, $new_file_path)) { echo 'success'; } else { $errors[] = 'Sorry, there was an error uploading your file!'; } } } else { $errors[] = 'You must upload an image!'; } }
  14. Alright so someone else was able to point out what I was doing wrong. The issue with was this line. I was uploading the original file, which I assumed was needed as the source. if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_file_path)) { The fix is this to change it to this. if(rename($new_image, $new_file_path)) { Here is my full code. It works now. if(isset($_FILES['fileToUpload']) AND !empty($_FILES['fileToUpload']["name"])) { if(is_uploaded_file($_FILES['fileToUpload']["tmp_name"])) { $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $source_file = $_FILES["fileToUpload"]["tmp_name"]; $random_name = generateRandomString(10); $new_image = $random_name . '.' . $imageFileType; $resized_image = compress_image($source_file, $new_image, 10); $new_file_path = $target_dir . $resized_image; if(!is_dir($target_dir)){ mkdir($target_dir, 0775, true); } $uploadOk = 1; // Check if image file is a actual image or fake image $check = getimagesize($source_file); if($check !== false) { // echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { $errors[] = 'File is not an image!'; $uploadOk = 0; } // Check if file already exists if (file_exists($target_file)) { $errors[] = 'Sorry, file already exists!'; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { $errors[] = 'Sorry, your file size is bigger than 5mb!'; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") { $errors[] = 'Sorry, only JPG, JPEG, PNG & GIF files are allowed!'; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if($uploadOk == 0) { $errors[] = 'Sorry, your file was not uploaded!'; // if everything is ok, try to upload file } else { if(rename($new_image, $new_file_path)) { echo 'success'; } else { $errors[] = 'Sorry, there was an error uploading your file!'; } } } else { $errors[] = 'You must upload an image!'; } }
  15. I expect the images to go to this directory. $target_dir = '../members/images/'.$global_user_id.'/projects/'.$url_project_id.'/'; And they do go to this directory. I see the images being uploaded with the correct name and extension. The only thing not happening is that those images are not being reduced in file size. They are the exact same size as the original image. Just to clarify, by compressing an image, I meant reducing the image file size. So if the original image file size is 1.6mb, then I want to reduce/compress that to a lower file size and upload it to the directory. I am assuming that's what that function does, unless I am wrong.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.