kmpx Posted September 28, 2010 Report Share Posted September 28, 2010 Estimados, tengo una función estructurada de la siguiente manera: <?php function crea_imagen($opcion_elegida) { header ('Content-type: image/png'); $im = @imagecreatetruecolor(850, 800) or die('NO HA SIDO POSIBLE CREAR LA IMAGEN'); colores(); flechas(); textos(); $y=0; $A=200; for($x=0;$x<450;$x++){ $x1=deg2rad($x); opciones($opcion_elegida); construir(); }/*----fin FOR---- */ imagepng($im); imagedestroy($im); return TRUE; } //fin creaimagen function colores() { // crea los colores a usar $fondoImg = imagecolorallocate($im, 211, 211, 211);//fondo gris imagefill($im,0, 0, $fondoImg); $negro = imagecolorallocate($im, 0, 0, 0); $rojo=imagecolorallocate($im, 255, 0, 0); $azul=imagecolorallocate($im, 0, 0, 255); $verde=imagecolorallocate($im, 0, 128, 0); } //fin colores function flechas() { // Crea ejes coordenados y flechas //imageline($im, X1, Y1, X2, Y2, Color de linea) imageline($im, 100, 100, 100, 650, $line_color);//linea vertical imageline($im, 90, 375, 700, 375, $line_color);//linea horizontal //Puntas de flecha $col_poly = imagecolorallocate($im, 0, 0, 0); // Dibuja la flecha eje Y $puntos = array( 100, 87, // Point 1 (x, y) 97, 100, // Point 2 (x, y) izq 103, 100 // Point 3 (x, y) der ); imagefilledpolygon($im, $puntos, 3, $linecolor); //Dibuja flecha eje X $puntos = array( 700, 370, // Point 1 (x, y) 710, 375, // Point 2 (x, y) 700, 380 // Point 3 (x, y) ); imagefilledpolygon($im, $puntos, 3, $linecolor); }//fin flechas function textos() { // Establece los textos de la imagen $font = './comic.ttf'; imagestring($im, 5, 5, 3, 'Serie de Fourier', $text_color); imagettftext($im, 10, 0, 89, 390, $line_color, $font, '0'); imagettftext($im, 10, 0, 72, 100, $line_color, $font, 'V(t)'); imagettftext($im, 10, 0, 700, 391,$line_color, $font, 't'); imagettftext($im, 10, 0, 89, 390, $line_color, $font, '0'); imagettftext($im, 10, 0, 72, 100, $line_color, $font, 'V(t)'); imagettftext($im, 10, 0, 700, 391,$line_color, $font, 't'); imagettftext($im, 10, 0, 80, 325,$line_color, $font, '50-'); imagettftext($im, 10, 0, 75, 275,$line_color, $font, '100-'); imagettftext($im, 10, 0, 75, 225,$line_color, $font, '150-'); imagettftext($im, 10, 0, 75, 175,$line_color, $font, '200-'); imagettftext($im, 10, 0, 75, 125,$line_color, $font, '250-'); } //fin textos function construir(){ imagesetpixel($im, $x+100, $y+300, $lineverde);//punto central imagesetpixel($im, $x+100, $y+299, $lineverde);//punto superior imagesetpixel($im, $x+101, $y+300, $lineverde);//punto derecho imagesetpixel($im, $x+100, $y+301, $lineverde);//punto inferior imagesetpixel($im, $x+99, $y+300, $lineverde);//punto izquierdo } // fin construir function opciones($opcion) { $op = $_POST['opcion']; switch($op) { case "pulso" : // Hacer todo lo de la opción 1 //pulsocuadrado(); //$funcion=-$A*sin($x1)+75;//se suman 75 para que coincida con el eje x $y=0; $A=200; for($x=0;$x<450;$x++){ $x1=deg2rad($x); $fou1=(-$A/3.1416); $fou2=(-$A/2)*sin($x1); $fou3=(2*-A/3*3.1416)*(cos ($x1)*2); $fou4=(2*-A/15*3.1416)*cos($x1)*4; $fou5=(2*-A/35*3.1416)*cos ($x1)*6; $suma=$fou1+$fou2+$fou3+$fou4+$fou5; $y=$suma;//sinusoidal roja break; case "dtesierra" : // Hacer todo lo de la opción 2 echo "Diente de sierra"; break; case "trian" : // Hacer todo lo de la opción 2 break; case "trianrect" : // Hacer todo lo de la opción 2 break; case "sierrarect" : // Hacer todo lo de la opción 2 break; default: break; // No hace nada } return TRUE; }// fin opciones // llamado a la funcion crea_imagen($_POST['opcion']); ?> Hago el llamado al final donde le paso por parámetro lo escogido en el formulario mediante <input radio>, pero nada! no se que cresta es ahora lo que toy haciendo mal, puesto que al querer mostrar solo me sale un simbolito: asi, en vez de generarme el dibujo completo Sugerencias al respecto??pliss Link to comment Share on other sites More sharing options...
rkstro Posted September 29, 2010 Report Share Posted September 29, 2010 Hay algunos varios motivos por lo que no se te crea la imagen, y te los detallo a continuación: ESTO: function opciones($opcion) { $op = $_POST['opcion']; switch($op) { DEBERIA SER: function opciones($opcion) { switch($opcion) { Otro, en la misma función opciones cierra el for del case "pulso". Lo siguiente es el gran problema que tienes, las funciones son contextos independientes el uno del otro, por lo que no puedes referenciar a una variable de otro lado, por ejemplo en construir usas $im que existe en crea_imagen() pero no en construir(), aqui tienes 2 o 3 formas de arreglarlo. 1- crear la variable $im como global (y otras mas tambien en que pasa lo mismo), esto lo haces agregando global $im; en la primera linea de cada funcion que use $im, quedando asi: function name_function() { global $im; 2.- Pasar por referencia en la funcion, la variable que usaras, por ejemplo: En la funcion crea imagen: function crea_imagen($opcion_elegida) { $im = @imagecreatetruecolor(850, 800); ... construir($im); La funcion construir() function construir($im){ imagesetpixel($im, $x+100, $y+300, $lineverde); 3.- No usar tanta funcion, y meter la mayor parte de lo que haces dentro de una funcion. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now