2011-06-15 1 views
-1

Salut Je veux redimensionner l'image lors de l'affichage sur la section des détails. Pour cela, j'ai vérifié plusieurs scripts PHP, mais cela ne fonctionne pas dans mes paramètres régionaux. Certains scripts montrant le chemin de l'image doivent commencer par/mais quand je donne comme ça, l'image montre n'est pas trouvée. Aussi la plupart des pages de démonstration de scripts montre de fausses images.Redimensionner l'image en utilisant PHP5 et JQuery

Répondre

2

La meilleure option est l'image de la culture en téléchargement d'images. Veuillez trouver ci-dessous le code pour la culture d'image en utilisant la bibliothèque de GD cela peut-il vous aider.

<?php 
function createThumb($upfile, $dstfile, $max_width, $max_height){ 
    $size = getimagesize($upfile); 
    $width = $size[0]; 
    $height = $size[1]; 
    $x_ratio = $max_width/$width; 
    $y_ratio = $max_height/$height; 
    if(($width <= $max_width) && ($height <= $max_height)) { 
      $tn_width = $width; 
      $tn_height = $height; 

    } elseif (($x_ratio * $height) < $max_height) { 
      $tn_height = ceil($x_ratio * $height); 
      $tn_width = $max_width; 

    } else { 
      $tn_width = ceil($y_ratio * $width); 
      $tn_height = $max_height; 

    } 
    if($size['mime'] == "image/jpeg"){ 

      $src = ImageCreateFromJpeg($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imageinterlace($dst, true); 
      ImageJpeg($dst, $dstfile, 100); 
    } else if ($size['mime'] == "image/png"){ 
      $src = ImageCreateFrompng($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      Imagepng($dst, $dstfile); 

    } else { 

      $src = ImageCreateFromGif($upfile); 
      $dst = ImageCreateTrueColor($tn_width, $tn_height); 
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 
      imagegif($dst, $dstfile); 
    } 
} 

//usage 

if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') { 
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 

    $imgNormal = time().$ext; 
    $normalDestination = "Photos/Orignal/" . $imgNormal; 
    $httpRootLarge = "Photos/Large/" . $imgNormal; 
    $httpRootSmall = "Photos/Small/" . $imgNormal; 
    $httpRootThumb = "Photos/Thumb/" . $imgNormal; 
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination); 
    createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image 
    createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image 
    createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image 
} 
?> 
+0

Avez-vous écrit cela vous-même? http://pastebin.com/Ed2YHV6w – mplungjan

0

Je pense que vous avez besoin d'ImageMagick. Vous pouvez trouver beaucoup d'exemples dans Google, je n'ai que l'exemple Perl.

1

Il y a deux options:

  1. créer une vignette de l'image (par la bibliothèque graphique en PHP) et le stocker (mieux pour la performance).
  2. Redimensionnez dynamiquement l'image avec la bibliothèque graphique PHP et affichez-la.

Vous pouvez également consulter cette page pour plus de détails: http://php.net/manual/book.image.php

Questions connexes