Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] GD obrazek + ramka
pejot
post
Post #1





Grupa: Zarejestrowani
Postów: 35
Pomógł: 0
Dołączył: 3.02.2008

Ostrzeżenie: (0%)
-----


Witam mam problem z GD. Chce aby przy tworzeniu miniatur dodawały mi się do nich ramki( wyglądu polaroida) ale jakoś topornie mi to idzie. Proszę was o pomoc bo sam już nie wiem co mam robić. Kod zaczyna się od 29 linijki.
WYPIS functions.php
  1. <?php
  2. ini_set('display_errors', 1);
  3. ?>
  4. <?php
  5. /*
  6.     Upload an image and create the thumbnail. The thumbnail is stored
  7.     under the thumbnail sub-directory of $uploadDir.
  8.     
  9.     Return the uploaded image name and the thumbnail also.
  10. */
  11. function uploadImage($inputName, $uploadDir)
  12. {
  13.    $image     = $_FILES[$inputName];
  14.    $imagePath = '';
  15.    $thumbnailPath = '';
  16.    $ramka = "imgages/ramka.png";
  17.    // if a file is given
  18.    if (trim($image['tmp_name']) != '') {
  19.        $ext = substr(strrchr($image['name'], "."), 1);
  20.  
  21.        // generate a random new file name to avoid name conflict
  22.        // then save the image under the new file name
  23.        $imagePath = md5(rand() * time()) . ".$ext";
  24.        $result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
  25.            
  26.        if ($result) {
  27.            // create thumbnail
  28.            $image1 = imagecreatefrompng( $ramka );  
  29.            $image2 = imagecreatefromjpeg( $image );
  30.            imagecopy( $image2, $ramka, 0, 0, 0, 0, 5, 10 );
  31.            imagejpeg( $image2 );
  32.            imagedestroy( $ramka );
  33.            imagedestroy( $image2 );
  34.            $thumbnailPath =  md5(rand() * time()) . ".$ext";
  35.            $image1 = imagecreatefrompng( $ramka );  
  36.            $image2 = imagecreatefromjpeg( $image );
  37.            imagecopy( $image2, $ramka, 0, 0, 0, 0, 5, 10 );
  38.            imagejpeg( $image2 );
  39.            imagedestroy( $ramka );
  40.            imagedestroy( $image2 );
  41.            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
  42.            
  43.            // create thumbnail failed, delete the image
  44.            if (!$result) {
  45.                unlink($uploadDir . $imagePath);
  46.                $imagePath = $thumbnailPath = '';
  47.            } else {
  48.                $thumbnailPath = $result;
  49.            }    
  50.        } else {
  51.            // the image cannot be uploaded
  52.            $imagePath = $thumbnailPath = '';
  53.        }
  54.        
  55.    }
  56.  
  57.    
  58.    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
  59. }
  60. header( "Content-type: image/jpg" );
  61. /*
  62.     Create a thumbnail of $srcFile and save it to $destFile.
  63.     The thumbnail will be $width pixels.
  64. */
  65. function createThumbnail($srcFile, $destFile, $width, $quality = 75)
  66. {
  67.    $thumbnail = '';
  68.    
  69.    if (file_exists($srcFile)  && isset($destFile))
  70.    {
  71.        $size        = getimagesize($srcFile);
  72.        $w           = number_format($width, 0, ',', '');
  73.        $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
  74.        
  75.        $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
  76.    }
  77.    
  78.    // return the thumbnail file name on sucess or blank on fail
  79.    return basename($thumbnail);
  80. }
  81.  
  82. /*
  83.     Copy an image to a destination file. The destination
  84.     image size will be $w X $h pixels
  85. */
  86. function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
  87. {
  88.    $tmpSrc     = pathinfo(strtolower($srcFile));
  89.    $tmpDest    = pathinfo(strtolower($destFile));
  90.    $size       = getimagesize($srcFile);
  91.  
  92.    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
  93.    {
  94.       $destFile  = substr_replace($destFile, 'jpg', -3);
  95.       $dest      = imagecreatetruecolor($w, $h);
  96.       //imageantialias($dest, TRUE);
  97.    } elseif ($tmpDest['extension'] == "png") {
  98.       $dest = imagecreatetruecolor($w, $h);
  99.       //imageantialias($dest, TRUE);
  100.    } else {
  101.      return false;
  102.    }
  103.  
  104.    switch($size[2])
  105.    {
  106.       case 1:       //GIF
  107.           $src = imagecreatefromgif($srcFile);
  108.           break;
  109.       case 2:       //JPEG
  110.           $src = imagecreatefromjpeg($srcFile);
  111.           break;
  112.       case 3:       //PNG
  113.           $src = imagecreatefrompng($srcFile);
  114.           break;
  115.       default:
  116.           return false;
  117.           break;
  118.    }
  119.    
  120.    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
  121.    //imagettftext($image, 9, 0, 100, 85, $color_czerwony, "font.ttf", "test");
  122.    switch($size[2])
  123.    {
  124.       case 1:
  125.       case 2:
  126.           imagejpeg($dest,$destFile, $quality);
  127.           break;
  128.       case 3:
  129.           imagepng($dest,$destFile);
  130.    }
  131.    return $destFile;
  132.  
  133. }
  134.  
  135. ...... itd
  136.  
  137. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 13:03