Advertisement
Guest User

Untitled

a guest
Aug 5th, 2011
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1.  
  2. <?php
  3. #user uploads file and it moves to server
  4. move_uploaded_file ($_FILES['Image']['tmp_name'], "/sites/".$_FILES['Image']['name']) or die ('Could not upload');
  5. ?>
  6.  
  7.  
  8. <?php
  9. #create image path
  10. $img = imagecreatefromjpeg("/sites/".$_FILES['Image']['name']);
  11. ?>
  12.  
  13.  
  14. <?php
  15. #pixelate function
  16. #if($img && imagefilter($img,IMG_FILTER_PIXELATE,4))
  17. #{
  18. echo 'Win <br/>';
  19. imagejpeg($img,'image.jpg',100);
  20. ?>
  21.  
  22. <?php
  23.  
  24. #resize image
  25.  
  26.     $output_width =30;
  27.     $output_height=50;
  28.     $xytotal = $output_width*$output_height;
  29.  
  30.     $path = '/sites/image.jpg';
  31.     $size_arr = getimagesize($path);
  32.     if ($size_arr[2]==IMAGETYPE_GIF)
  33.         $image = imagecreatefromgif($path);
  34.     else if ($size_arr[2]==IMAGETYPE_JPEG)
  35.         $image = imagecreatefromjpeg($path);
  36.     else if ($size_arr[2]==IMAGETYPE_PNG)
  37.         $image = imagecreatefrompng($path);
  38.     else
  39.         die_default_image();
  40.  
  41.     $tmpname = tempnam( sys_get_temp_dir() , "phptmp");
  42.  
  43.     resize_image($tmpname, $image, $size_arr, $output_width, $output_height);
  44.  
  45.     $img = imagecreatefromjpeg($tmpname);
  46.     imagejpeg($img,'image.jpg',100);
  47.    
  48.     unlink( $tmpname );
  49.    
  50. #loop to populate rgb values and save to array: $xy
  51.    
  52.     $imagew = imagesx($img);
  53.     $imageh = imagesy($img);
  54.     $xy = array(i);
  55.    
  56.     echo "Image (w,h): ($imagew, $imageh)<br/>";
  57.    
  58.     $x = 0;
  59.     $y = 0;
  60.     for ($x = 0; $x <= $imagew; $x++) {
  61.     for ($y = 0;$y <= $imageh; $y++ ) {
  62.             $rgb = imagecolorat($img, $x, $y);
  63.             $r = ($rgb >> 16) & 0xFF;
  64.             $g = ($rgb >> 8) & 0xFF;
  65.             $b = $rgb & 0xFF;
  66.            
  67.             #loop to save ($r,$g,$b) into $xy
  68.             for ($i = 0; $i <= $xytotal; $i++) {
  69.             $xy[i] = ($r, $g, $b);
  70.             }
  71.            
  72.        
  73.            
  74.             echo "xy: $xy x: $x, y: $y <br/>";
  75.             var_dump($r, $g, $b);
  76.         }
  77.     }
  78.    
  79. function die_default_image()
  80. {
  81.     //43byte 1x1 transparent pixel gif
  82.     header("content-type: image/gif");
  83.     echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
  84. }
  85.  
  86. function resize_image($thumbname, $image, $size_arr, $max_width, $max_height)//maintain aspect ratio
  87. {
  88.     $width  = $max_width;
  89.     $height = $max_height;
  90.     list($width_orig, $height_orig, $img_type) = $size_arr;
  91.     $ratio_orig = $width_orig/$height_orig;
  92.  
  93.     if ($width/$height > $ratio_orig) {
  94.        $width = floor($height*$ratio_orig);
  95.     } else {
  96.        $height = floor($width/$ratio_orig);
  97.     }
  98.  
  99.     // Resample
  100.     $tempimg = imagecreatetruecolor($width, $height);
  101.     imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  102.     imagejpeg($tempimg, $thumbname, 80);
  103. }
  104.  
  105. if ( !function_exists('sys_get_temp_dir')) {
  106.   function sys_get_temp_dir() {
  107.     if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
  108.     if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
  109.     if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
  110.     $tempfile=tempnam(uniqid(rand(),TRUE),'');
  111.     if (file_exists($tempfile)) {
  112.     unlink($tempfile);
  113.     return realpath(dirname($tempfile));
  114.     }
  115.   }
  116. }
  117. ?>
  118.  
  119.  
  120. <br>
  121. <br>
  122. <img src="http://localhost/image.jpg" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement