PHP - Image Save As With A Lower Quality
Hi, I am tryng to use the gd functions in php to output an image of a lower quality and save it as another image:
Code: [Select] header('Content-Type: image/jpeg'); $im = @imagecreatefromjpeg('SDC10424.JPG'); imagejpeg($im, null, 50);// this line lowers the image quality imagedestroy($im); but the problem is how to save it? I tried file_put_contents, but it didnt work, like fileputcontents("test.jpg", $im); Similar TutorialsHello. I was wondering if there is any way to remove the white pixels from around a transparent png image? When I use imageimagecopymerge to place a transparent PNG on top of another image with a dark background, the top image has a few white pixels around the border. Is there any way to remove these white pixels? I attached an example of the image with the white pixels. Thanks This resizer works fine except I would like it to save the original image AND the thumbnail. Right now its only saving the cropped thumbnail. It's seems simple enough but I've tried several different ways but can't get it to work. :( I would appreciate your help. :) define( 'DESIRED_IMAGE_WIDTH', 150 ); define( 'DESIRED_IMAGE_HEIGHT', 150 ); $source_path = $_FILES[ 'thumb' ][ 'tmp_name' ]; $timestamp = time(); $target = "image_files/".$imagename; move_uploaded_file($source, $target); // // Add file validation code here // list( $source_width, $source_height, $source_type ) = getimagesize( $source_path ); switch ( $source_type ) { case IMAGETYPE_GIF: $source_gdim = imagecreatefromgif( $source_path ); break; case IMAGETYPE_JPEG: $source_gdim = imagecreatefromjpeg( $source_path ); break; case IMAGETYPE_PNG: $source_gdim = imagecreatefrompng( $source_path ); break; } $source_aspect_ratio = $source_width / $source_height; $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT; if ( $source_aspect_ratio > $desired_aspect_ratio ) { // // Triggered when source image is wider // $temp_height = DESIRED_IMAGE_HEIGHT; $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio ); } else { // // Triggered otherwise (i.e. source image is similar or taller) // $temp_width = DESIRED_IMAGE_WIDTH; $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio ); } // // Resize the image into a temporary GD image // $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height ); imagecopyresampled( $temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height ); // // Copy cropped region from temporary image into the desired GD image // $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2; $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2; $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); imagecopy( $desired_gdim, $temp_gdim, 0, 0, $x0, $y0, DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT ); // // Render the image // Alternatively, you can save the image in file-system or database // header( 'Content-type: image/jpeg' ); imagejpeg( $desired_gdim, "image_files/" . $timestamp . $_FILES["thumb"]["name"] ); hi all, i'm trying to save an image after cropping it using jcrop (jquery plugin). i'm currently using a script i found on the internet called ImageManipulation.php (attached). its working fine, but i get a black image if i crop a gif, or png images (works great for jpg images). Code: [Select] $objImage = new ImageManipulation("../products/" . $_SESSION['image']); if ($objImage->imageok) { // get x, y, w, h from hidden inputs that are update by jcrop $objImage->setCrop($_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']); $objImage->resize(150); $objImage->save("../products/" . $_SESSION['image']); } how can i fix this?? if you have a better way of using php with jcrop to save cropped images please share it thanks for the help Hi I have a javascript image cropping script which is working well, I then have some php to save the new cropped image. But I can't seem to get it working 100%. When the form is posted the cropped image is not created and I get a "The connection was reset" error. If I comment out the line Code: [Select] $source=imagecreatefromjpeg($image); then I dont get the The connection was reset" error but the image is obviously still not created. Can anyone help? Code: [Select] <?php function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){ list($imagewidth, $imageheight, $imageType) = getimagesize($image); $imageType = 'image/jpeg'; $newImageWidth = ceil($width * $scale); $newImageHeight = ceil($height * $scale); $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); switch($imageType) { case "image/jpeg": case "image/jpg": $source=imagecreatefromjpeg($image); break; } imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height); switch($imageType) { case "image/jpeg": case "image/jpg": imagejpeg($newImage,$thumb_image_name,90); break; } chmod($thumb_image_name, 0777); return $thumb_image_name; } function getHeight($image) { $size = getimagesize($image); $height = $size[1]; return $height; } function getWidth($image) { $size = getimagesize($image); $width = $size[0]; return $width; } $large_image_location = $upload_path.$large_image_name.'.jpg'; $thumb_image_location = $upload_path.$thumb_image_name.'.jpg'; if (isset($_POST["upload_thumbnail"])) { $x1 = $_POST["x1"]; $y1 = $_POST["y1"]; $x2 = $_POST["x2"]; $y2 = $_POST["y2"]; $w = $_POST["w"]; $h = $_POST["h"]; $scale = $thumb_width/$w; $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale); header("location:index.php"); exit(); } ?> I'm trying to save images from a directory into mine. To get the image I am having to take the email from a database, split it and take whatever it is before the '@' sign and add it to "-S.jpg". I wrote the script and when I echo the variable it shows the correct thing, but when it tries to save it , it is trying to find the image as "script>-S.jpg". It looks like it is taking whatever is after the last '/' which in the variable since I am running javascript it is going to be </script> if you look at my variable $url. Here is the code below. Any help is appreciated. while($rows=mysql_fetch_array($result)){ $email=$rows['email']; $url= "<SCRIPT LANGUAGE=\"javascript\"> var url; var email = \"$email\"; function emailsplit () { var userid = email.split(\"@\"); var url = userid[0]; var imgid = \"http://my.snu.edu/images/idpictures/\" + url + \"-S.jpg\"; return url; } document.write(emailsplit()); </script> "; $img[]= 'http://my.snu.edu/images/idpictures/'.$url.'-S.jpg'; } function save_image($img,$fullpath='basename'){ if($fullpath=='basename'){ $fullpath = basename($img); } $ch = curl_init ($img); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec($ch); curl_close ($ch); if(file_exists($fullpath)){ unlink($fullpath); } $fp = fopen($fullpath,'x'); fwrite($fp, $rawdata); fclose($fp); } foreach($img as $i){ save_image($i); if(getimagesize(basename($i))){ echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>'; }else{ echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>'; } } Hi Guys, Please can someone here help me with a simple browse for an image from the computer and then when the form is submitted it uploads the image and then saves the image url in my DB. my form page is here http://bit.ly/gntCvD Any help will be appreciated. thanks Craig How can i save image just assume that i have "test.jpg" and wish to store the image to directory "directory A" using Curl..how can i do that? thanks in adv Hi all, i need little help in saving and download gif image created by using GD library. code works fine in mozilla and crome but in IE a new new page where the image is created,saved as well as dowloaded remains open and shows this"Action canceled". but the image is stored and downloaded on my system. i just want to close this page automatically when download is complete Code: [Select] <?php ob_start(); ini_set("memory_limit","180M"); $data = explode(",", $_POST['img']); $width = $_POST['wp']; $height = $_POST['ht']; $image=imagecreatetruecolor( $width ,$height ); $background = imagecolorallocate( $image ,0 , 0 , 0 ); //Copy pixels $i = 0; for($x=0; $x<=$width; $x++){ for($y=0; $y<=$height; $y++){ $int = hexdec($data[$i++]); $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int); imagesetpixel ( $image , $x , $y , $color ); } } //Output image and clean header( "Content-type: image/gif" ); $name = rand(0,9999); $n = $name; $name = "img/".$name.".gif"; ImageGIF($image,$name,100); imagedestroy($image); $path = "http://localhost/madeup/img/"; $fullPath = $path.$n.'.gif'; if ($fd = fopen ($fullPath, "r")) { header( "Content-type: image/gif" ); header("Content-Disposition: attachment; filename=".$n); header("Cache-control: private"); while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); ob_end_flush(); exit; ?> My current project requires me to save the number of views an image (banner ad) has appeared on users website. The problem here is that the image will be appearing on several users website and i want to be able to save the number of times it has appeared on my database. I know how to do this if the image was on the same server as the database, but not when the image and database are on completely different servers. This is somewhat like analytic where it tracks the visitors to a page. Does anyone how i can accomplish this? i figured i would need to create a JavaScript code to provide to the user so they can place it on the site but how do i get that code to connect to my database? So. I'm using this code right he <?php $imagepath = "css/images/statspage.jpg"; $image = imagecreatefromjpeg($imagepath); $imgheight = imagesy($image); $color = imagecolorallocate($image, 255, 255, 255); imagestring($image, 5, 70, $imgheight-50, "This is a test", $color); header('Content-Type: image/jpeg'); imagejpeg($image); ?> It works like it should. Now my problem is, is that the image is being displayed at a very low quality! Here's the original image: Here's the image with the code above: RIGHT away you can tell the HUGE difference. I'm not sure what I'm doing wrong. Maybe i need a higher res image? If y'all guys know a different method THAT also achieves the same thing I'm trying to do. Please let me know ASAP! Hey guys! I have the following script to create an excel file, the thing is that I dont want to be asked if I want to open or save the file when accessing the php file...I just want the php file directly to save the excel file. Heres the code: $filename = "test.xls"; $contents = "testdata1 \ntestdata2 \ntestdata3 \n"; header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename='.$filename); echo $contents; Is there a way instead of using the header function, use something like: fopen, fwrite, fclose ? Thanks in advance! Cheers, Hi all, I am creating a thumbnail from an uploaded image (in this case a company logo). The problem is that the thumbnail image is poor quality and not fit to display on the website! I have attached the uploaded image and then then generated thumbnail, so you can see how much it is degrading. I always want the thumb to be the same size, which is 129pxx44px. I have included the thumb generation part of my script below: // This is declared at the start of the script $tdir = "images/agencylogos/thumbs/"; $twidth = "129"; $theight = "44"; // This is the actual part of the script that does the resizing $simg = imagecreatefromjpeg($newname); $currwidth = imagesx($simg); $currheight = imagesy($simg); if ($currheight > $currwidth) { $zoom = $twidth / $currheight; $newheight = $theight; $newwidth = $currwidth * $zoom; } else { $zoom = $twidth / $currwidth; $newwidth = $twidth; $newheight = $currheight * $zoom; } $dimg = imagecreate($newwidth, $newheight); imagetruecolortopalette($simg, false, 256); $palsize = ImageColorsTotal($simg); imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $image_name, 100); imagedestroy($simg); imagedestroy($dimg); Can anyone see any ways of improving this script so that it would make the thumb image better quality? Many thanks, Greens85 This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=321638.0 I'm having a mini application developed to create A4 PDF's using PHP A key part of the project needs to be able to insert high quality graphics into the document that when printed out look really sharp Some of the graphics are variable and don't always have to be printed I just wanted to know if there was a general guide on what graphics and quality to use? The graphics I need printed print really well when put into a Word document at 300 resolution - so I can't see why there should be any problems inserting into a PDF - these graphics are PNG ones Unfortunately, if I insert the same PNG format into the PDF via the PHP code, the graphic turns out to be REALLY big Just looking for advice Thanks OM i want to make a simple forced lower case form but i got stuck on it
to begin with i want to everything user's write and submit convert to lowercase
am i using the right code? i'm not sure about using preg_match in this situation but i dont know what else to do don't be harsh with me i'm noob <?php if(isset($_GET['user'])&&!empty($_GET['user'])) { $user = $_GET['user']; if (preg_match('$englishUpperAlphabet'//English alphabet Upper case array//',$user)){ $user = mb_strtolower($user); echo $user; } else{ echo 'none'; }} ?> <form action="t.php" action="GET"> name: <input type="text" name="user" > <br> <input type="submit" value="submit" > </form> Im displaying scores from my database from highest to lowest but 10 always comes out first. Why? Is there any way to access a variable that is not assigned until further down the page eg: echo $myname; $myname = 'batman'; // clearly my real name which is why I am so poor at coding Thanks Hello, I've got what should be a simple solution here. I've tried many things but it just won't work for me. This is what I have. I've got a wysiwyg editor setup to write to a file called news.php. The file that has the editor is edit/index.php. It's just a simple textarea. The textarea is supposed to include news.php. For some reason, it won't let me include that file that is in a lower directory. The file that writes to news.php is called edit/write.php. edit/write.php doesn't write to news.php instead it writes to edit/news.php. The only reason I want to include the lower directory is so that I can put a .htaccess password protection on the edit directory. I'll include some code below. Code: (edit/index.php) [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <head> <title>Editing News</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="nicEdit.js"></script> <script type="text/javascript"> bkLib.onDomLoaded(function() { nicEditors.allTextAreas() }); </script> <form method="post" action="write.php"> <p> <textarea name="content" style="width:100%;height:1000px;"><?php include '../news.php';?></textarea> <input type="submit" value="Save" /> </p> </form> </body> </html> Code: (edit/write.php) [Select] <?php $File = "../news.php"; $Handle = fopen($File, 'w'); $Data = $_POST['content']; fwrite($Handle, $Data); fclose($Handle); header("Location: index.php"); ?> index.php is just a simple include news.php. Am I missing an easy step? Any help would be great!! Thanks! I mostly if not always name my classes with the first letter capitalized.
I typically use enable autoloading as follows. I don't know when I started converting the class name to under-case. As such, however, I need to make the filenames undercase.
Is this the correct approach?
spl_autoload_register(function ($class) { $class=strtolower($class); if(file_exists(__DIR__.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$class.'.php')) { require_once __DIR__.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.$class.'.php'; } }); Hi, I wonder if some one could help me. I am trying to set up a few rules to make sure that users create safe passwords on my site: I have the below check which makes sure that the user enters a word of at least 8 charachters long: Code: [Select] if( strlen($password) < $MaxPwdLength ){ $errmsg_arr[] = 'Password must be at least 8 charachters long '; $errflag = true; } Does anyone know how I could write an if function to make sure that the ser is entereing a password with at least one capital and one lower case letter in. Hope you can help. Thanks Ed |